How to Use a Custom Entity Class to Stored Procedure Using Linq-To-SQL


This article demonstrates how to use a Customer Entity Class to stored procedure using Linq-To-SQL.

Getting Started:

  1. Create a new website in asp.net
  2. Put website name and browse save location
  3. Click OK.

Create a new database and make a new table and add some columns.

img1.jpg

Figure 1.

Insert some data in table.

img2.jpg

Figure 2.

Create a new stored procedure and select records to show on page.

img3.jpg

Figure 3.

Add a new Linq to SQL Classes in application.


img4.jpg

Figure 4.

If a data connection is not added then add a new data connection in the Server Explorer.


img5.jpg

Figure 5.

Configure a data connection with server name, authentication and database.


img6.jpg

Figure 6.

Now drag and drop a stored procedure onto the LINQ to SQL Classes.


img7.jpg

Figure 7.

Now add a new entity class called customers using right-click into LINQ-To-SQL data context.

img8.jpg

Figure 8.

Add properties using right-click and click Add Properties.

img9.jpg

Image 9.

img10.jpg

Figure 10.

Now time to bind the stored procedure with the customer class; right-click on the stored procedure and click on properties and select class name in ReturnType.

img11.jpg

Figure 11.

Now drag and drop GridView control onto the page.

<h2>
        Customers</h2>
<p>

<asp:GridViewID="CustomersGridView"runat="server">
</
asp:GridView>

</p>

protectedvoidPage_Load(object sender, EventArgs e)
    {
if (!Page.IsPostBack)
        {           
using (CustomersDataClassesDataContext customers = newCustomersDataClassesDataContext())
            {
List<CustomersList>CustomersList = customers.GetAllCustomers().ToList<CustomersList>();
CustomersGridView.DataSource = CustomersList;
CustomersGridView.DataBind();
            }
        }
    }

All set; now build and run the website to see the result.

img12.jpg

Figure 12.

Up Next
    Ebook Download
    View all
    Learn
    View all