In this article I will explain with an example, how to get
Output parameter from
Stored Procedure using
Entity Framework in Windows Forms (WinForms) Application using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts following parameters which are used to
insert the records:
Name – This is an INPUT Parameter to pass the Name of the Customer.
Country – This is an INPUT Parameter to pass the Country of the Customer.
CustomerId – This is an OUTPUT Parameter to get last inserted CustomerId.
Note: Output Parameter is identified by the keyword OUTPUT.
CREATE PROCEDURE [Customers_InsertCustomer_Output]
@Name VARCHAR(100),
@Country VARCHAR(50),
@CustomerId INT OUTPUT
AS
BEGIN
INSERT INTO [Customers]
([Name]
,[Country])
VALUES
(@Name
,@Country)
SELECT @CustomerId =SCOPE_IDENTITY()
END
Entity Framework Model
Once the
Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: You will notice that None option is selected, because the Stored Procedure does not return any value.
Form Design
The following Form consists of:
Label – For labelling controls.
TextBox – For capturing Name to be inserted.
ComboBox – For capturing Country Name to be inserted.
Button – For inserting records.
The Button has been assigned with a Click event handler.
Namespace
You will need to import the following namespace.
C#
using System.Data.Objects;
VB.Net
Imports System.Data.Objects
Populating ComboBox with Country names
Inside the Form Load event handler, the ComboBox items are added.
C#
private void Form1_Load(object sender, EventArgs e)
{
cbCountries.Items.Add("United States");
cbCountries.Items.Add("India");
cbCountries.Items.Add("France");
cbCountries.Items.Add("Russia");
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cbCountries.Items.Add("United States")
cbCountries.Items.Add("India")
cbCountries.Items.Add("France")
cbCountries.Items.Add("Russia")
End Sub
Getting Output Parameter from Stored Procedure using Entity Framework
When Insert button is clicked, the Name and Country value are fetched from their respective fields.
An object of AjaxSamplesEntities is created and ObjectParameter class for CustomerId is initiated.
Finally, the inserted CustomerId is fetched from the ObjectParameter value and the TextBox and ComboBox value is set to empty and CustomerId is displayed in MessageBox.
C#
private void OnInsert(object sender, EventArgs e)
{
string name = txtName.Text;
string country = cbCountries.SelectedItem.ToString();
using (AjaxSamplesEntities entities = new AjaxSamplesEntities())
{
ObjectParameter param = new ObjectParameter("CustomerId", typeof(int));
entities.Customers_InsertCustomer_Output(name, country, param);
string customerId = Convert.ToInt32(param.Value).ToString();
txtName.Text = string.Empty;
cbCountries.Text = string.Empty;
MessageBox.Show("Inserted Customer ID: " + customerId);
}
}
VB.Net
Private Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Dim name As String = txtName.Text
Dim country As String = cbCountries.SelectedItem.ToString()
Using entities As AjaxSamplesEntities = New AjaxSamplesEntities()
Dim param As ObjectParameter = New ObjectParameter("CustomerId", GetType(Integer))
entities.Customers_InsertCustomer_Output(name, country, param)
Dim customerId As String = Convert.ToInt32(param.Value).ToString()
txtName.Text = String.Empty
cbCountries.Text = String.Empty
MessageBox.Show("Inserted Customer ID: " & customerId)
End Using
End Sub
Screenshots
Record after Insert in database
Downloads