Calling an stored procedure using ADO.NET Entity Model

Create a new website in your solution. Now add a new project as a class library named Business Layer. Add a new project again and name it DAL.
Add the reference of BusinessLayer to the website project. Similarly, add the reference of the DAL project to BusinessLayer. This all we are doing as we are trying to create a layered architecture for the sample application.
Now right click on the DAL project and add a new item -> ADO.NET Entity Model click ok.
Now it'll ask you to configure your Model from the database. Here I'm skipping this step assuming that you may know how to configure the model using the wizard.
After configuration, your model will be added and opened and will show all your tables you have select from the wizard.
  1. Now create a stored Procedure in your database.
  2. Come back to your solution and open the sampleModel.edmx from the DAL project and right-click.
  3. Click on "Update the model from database" it'll open the same wizard that was appeared while you were adding the Model. But this time it'll only show the newly added item in the database.
  4. Expand the stored procedure item and select your stored procedure from the list.
    Now click finish and save the Model.

  5. Go to the visual studio menu and select View->Other Windows -> Entity Data Model Browser
  6. Now open the Model Browser and expand Entity Container and right-click on "Function Imports" and click "Add Function Import…".
  7. Name the function "GetAllEmployees" and select the available stored procedure in the model from the dropdown list GetAllEmployees. Now select the return type so you are going to show the employee details so the return type would be the Entity: Employee
  8. Click on and save the model.
Now write the code in your business layer to get the returned data from the Stored Procedure.
  1. using DAL;  
  2. namespace BusinessLayer  
  3. {  
  4.     public class Employees  
  5.     {  
  6.         public static List<Employee> GetAllEmployeesInfo()  
  7.         {  
  8.             btnetEntities data = new btnetEntities();  
  9.             var EmpCol = data.GetAllEmployees();  
  10.             var EmpItems = from e in EmpCol  
  11.                            select new Employee  
  12.                            {  
  13.                                Eid = e.employee_id,  
  14.                                Name = e.employee_name,  
  15.                                EMail =  
  17.                            };  
  18.             List<Employee> ls = new List<Employee>(EmpItems.ToList());  
  19.             return ls;  
  20.         }  
  21.     }  
In your aspx page bind the grid:
  1. using BusinessLayer;  
  3. namespace EFLinqToSqlDemo  
  4. {  
  5.     public partial class _Default : System.Web.UI.Page  
  6.     {  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.             GridView1.DataSource = Employees.GetAllEmployeesInfo();  
  10.             GridView1.DataBind();  
  11.         }  
  12.     }  
And you are done.:))
Here's the output.

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles