Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
Hi,
I am new to Asp.net. How to use stored procedures in entity framework ? As i am using a stored procedure of complex type to get data and displaying it in a grid.The SP takes single parameter.
Can anyone give me the steps for doing this both in Asp.net and in MVC4.
Posted
Updated 19-Jul-13 6:22am
v2
Comments
[no name] 19-Jul-13 12:26pm    
http://www.bing.com/search?q=How+to+use+stored+procedures+in+entity+framework

Go to Model Browser
-> Go To your Model Store
-> Expand Stored Procedures
-> Right Click On your stored Procedure there
-> Select Create Function Import


OR

Go to Go to Model Browser
-> Go To your Model
-> Expand Your Entity Container
-> Right Click Imports
-> Select Create Function Import
-> Select the Stored Procedure you want to use
-> Provide any name of your function
-> Choose your return type of your Stored Procedure , if its is not returning anything choose None
-> Click Ok

Any Prob Comment plz
 
Share this answer
 
Comments
nam3699 19-Jul-13 13:31pm    
I already have an existing import function say (Student) which is specified as (returns a collection: complex type) with name say (StudentR), I am using it to get the data into a grid. Now how do i call this function as it is a complex type?.
Wrote the code as below and it worked

In Button click

XML
protected void Button1_Click(object sender, EventArgs e)
{
    Student_BL scl = new Student_BL();
   Guid gid = Guid.Parse(TextBox1.Text);
    var y = scl.getstudent(gid);
    GridView1.DataSource = (y.ToList<StudentR>());
    GridView1.DataBind();
}


The Business layer for student is.

XML
public class Student_BL
{
    SS1DBEntities2 obj = new Ss1DBEntities2();

    public List<StudentR>  getstudent(Guid gid)
    {
        System.Data.Objects.ObjectParameter Student1 = new       System.Data.Objects.ObjectParameter("studentid",gid);
        var x = obj.Student(Student1);
        return x.ToList<StudentR>();      
     }

}


Context class is

SQL
public virtual ObjectResult<StudentR> Student(ObjectParameter studentId)
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<StudentR>("Student", studentId);
    }
 
Share this answer
 
v2
Here is a sample i'm providing,I've Used oyee table of Northwind database
This is the way you can bind complex type function to gridview.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = EmployeeByCountry("UK");
        GridView1.DataBind();
    }


List<Employees> EmployeeByCountry(string Country)
   {
       List<Employees> EmpListObj = new List<Employees>();
       NORTHWNDEntities NWEObj= new NORTHWNDEntities();
       EmpListObj = NWEObj.EmployeeByCountry(Country).ToList<Employees>();
       return EmpListObj;
   }


-------
Regards
Bikash
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900