In this article I will explain with an example, how to call and execute a Stored Procedures in Entity Framework in ASP.Net using C# and VB.Net.
 
 

Database

Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 

Stored Procedure

The following Stored Procedures @ContactName parameter which is used to perform a search on the records in Customers Table.
CREATE PROCEDURE Customers_SearchCustomers
       @ContactName NVARCHAR(30)
AS
BEGIN
      SET NOCOUNT ON;
      SELECT TOP 10 CustomerId
            ,ContactName
            ,City
            ,Country
      FROM Customers
      WHERE  ContactName LIKE '%' + @ContactName + '%'
END
 
 

Configuring Entity Framework

You will need to configure the Entity Framework in order to connect to the database.
Note: For more details on how to configure and connect Entity Framework to database, please refer my article Configure Entity Framework Step By Step in ASP.Net.
 
 

Configuring Stored Procedure in Entity Framework Model

Once the Entity Framework is configured, the next step is to import the Stored Procedures in the Entity Framework model.
In order to do so, you will need to Right Click the Entity Model and select Update Model from Database option from the context menu.
Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
Then, inside the Update Wizard, check (select) the Stored Procedure and click Finish.
Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
Now we will need to import the Stored Procedure into the Entity Framework so that it can be called as a Function using Entity Framework.
Thus, you will need to Right Click the Entity Model, click Add New and then click Function Import.
Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
This will open the Add Function Import dialog window. Here first you need to specify the Function Import Name which is the name of the function used to call the Stored Procedure and then, select the Stored Procedure that will be executed when the function is called.
The Return Type is selected as Entities which is the Customer Entity class.
Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
Finally, you will see the function name in the Model Browser.
Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For Search Customer Name.
GridView – For displaying data.
The GridView consists of Four BoundField columns.
Customer Name:
<asp:TextBox ID="txtCustomerName" runat="server" />
<asp:Button Text="Search" runat="server" OnClick="SearchCustomers" />
<br />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 

Executing Stored Procedure using Entity Framework

Inside the Page Load event handler, the GridView is populated with the records returned from the Stored Procedure, where the SearchCustomers is called by passing the parameter value blank which populates all the records.
When the Search Button is clicked, the SearchCustomers function is called by passing the value of the TextBox to the functions which ultimately returns filtered records.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        NorthwindEntities entities = new NorthwindEntities();
        gvCustomers.DataSource entities.SearchCustomers(string.Empty);
        gvCustomers.DataBind();
    }
}
 
protected void SearchCustomers(object sender, EventArgs e)
{
    NorthwindEntities entities = new NorthwindEntities();
    gvCustomers.DataSource entities.SearchCustomers(txtCustomerName.Text.Trim());
    gvCustomers.DataBind();
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim entities As New NorthwindEntities()
        gvCustomers.DataSource entities.SearchCustomers(String.Empty)
        gvCustomers.DataBind()
    End If
End Sub
 
Protected Sub SearchCustomers(sender As Object, e As EventArgs)
    Dim entities As New NorthwindEntities()
    gvCustomers.DataSource entities.SearchCustomers(txtCustomerName.Text.Trim())
    gvCustomers.DataBind()
End Sub
 
 

Screenshot

Call and execute a Stored Procedure in Entity Framework in ASP.Net using C# and VB.Net
 
 

Demo

 
 

Downloads