In this article I will explain with an example, how to get Output parameter from Stored Procedure using Entity Framework in ASP.Net using C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Get Output Parameter from Stored Procedure using Entity Framework in ASPNet using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure will be used to Insert data into the SQL Server database table.
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: For beginners in Windows Forms and Entity Framework, please refer my article Entity Framework Database First Approach in Windows Forms using C# and VB.Net. It covers all the information needed for connecting and configuring Entity Framework.
 
Get Output Parameter from Stored Procedure using Entity Framework in ASPNet using C# and VB.Net
 
Following Stored Procedure is imported as Function.
Get Output Parameter from Stored Procedure using Entity Framework in ASPNet using C# and VB.Net
 
Note: You will notice that None option is selected, because the Stored Procedure does not return any value.
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing Name to be inserted.
DropDownList – For selecting Country name to be inserted.
Button – For inserting the record.
The Button has been assigned with an OnClick event handler.
<table>
    <tr>
        <td>Name:</td>
        <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Country:</td>
        <td>
            <asp:DropDownList ID="ddlCountries" runat="server">
                <asp:ListItem Text="Please select" Value=" "></asp:ListItem>
                <asp:ListItem Text="United States" Value="United States"></asp:ListItem>
                <asp:ListItem Text="India" Value="India"></asp:ListItem>
                <asp:ListItem Text="France" Value="France"></asp:ListItem>
                <asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
            </asp:DropDownList></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="OnInsert" /></td>
    </tr>
</table>
 
 

Namespaces

You will need to import the following namespace.
C#
using System.Data.Entity.Core.Objects;
 
VB.Net
Imports System.Data.Entity.Core.Objects
 
 

Getting Output Parameter from Stored Procedure using Entity Framework in ASP.Net

When Insert Button is clicked, the Name and Country values are fetched from their respective fields.
An object of AjaxSamplesEntities is created and then an object of type ObjectParameter class is created for fetching the CustomerId Output parameter.
Then, the Stored Procedure is called using Entity Framework and Name, Country are passed along with the ObjectParameter for fetching the returned CustomerId.
Finally, the value of insertd CustomerId is retrieved from the ObjectParameter and displayed using JavaScript Alert MessageBox using RegisterStartupScript method.
C#
protected void OnInsert(object sender, EventArgs e)
{
    string name = txtName.Text;
    string country = ddlCountries.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;
        ddlCountries.Text = string.Empty;
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
    }
}
 
VB.Net
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
    Dim name As String = txtName.Text
    Dim country As String = ddlCountries.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
        ddlCountries.Text = String.Empty
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
    End Using
End Sub
 
 

Screenshots

The Form

Get Output Parameter from Stored Procedure using Entity Framework in ASPNet using C# and VB.Net
 

Record after Insert in database

Get Output Parameter from Stored Procedure using Entity Framework in ASPNet using C# and VB.Net
 
 

Downloads