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.
Configuring Stored Procedure in 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.
Then, inside the Update Wizard, check (select) the Stored Procedure and click Finish.
Thus, you will need to Right Click the Entity Model, click Add New and then click Function Import.
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.
Finally, you will see the function name in the Model Browser.
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
Demo
Downloads