In this article I will show you step by step roadmap of creating an ASP.NET application in which we will perform CRUD using LINQ to SQL assuming that you have prior basic knowledge of LINQ.
Let's begin!
Step 1: Let's create a table UserDetails to perform CRUD operations with the following attributes and types.
Step 2: Go to project and Add, New Item, then Data and select LINQtoSQL Class.
Step 3: New Window with Server Explorer opens, now click on server explorer.
Step 4: Add New Connection.
Step 5: Provide the Server name and Database name, here we are using local database (Windows Authentication).
Step 6: Test Connection.
Step 7: Selected database will be displayed in Server Explorer.
Step 8: You can see the table under selected database.
Step 9: Now drag and drop that table over DataClasses1.dbml. Here table name converted into a typical C# class and its attributes converted into properties.
Step 10: Now add a webform to retrieve that table data.
Step 11: Add GridView data tool from the toolbox.
Added buttons for Select, Insert, Update and Delete.
Step 12: You can check the automatically generated connection string in Web.Config.
Step 13: Check DataClasses1DataContext class, which is the entry point.
Apply the following snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace JQueryImageRotator
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
- }
-
- private void RetrieveUserDetails()
- {
- DataClasses1DataContext dtContext = new DataClasses1DataContext();
- GridView1.DataSource = dtContext.UserDetails;
-
-
-
- GridView1.DataBind();
- }
-
- protected void btnSelect_Click(object sender, EventArgs e)
- {
- RetrieveUserDetails();
- }
-
- protected void btnInsert_Click(object sender, EventArgs e)
- {
- using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
- {
- UserDetail Udetails = new UserDetail {
-
- UserName="Shridhar",
- City="New Delhi",
- Designation="SE"
- };
-
- dtContext.UserDetails.InsertOnSubmit(Udetails);
- dtContext.SubmitChanges();
- }
- RetrieveUserDetails();
- }
-
- protected void btnUpdate_Click(object sender, EventArgs e)
- {
- using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
- {
- UserDetail Udetails = dtContext.UserDetails.SingleOrDefault(x => x.UserId == 6);
- Udetails.Designation = "Leader";
- dtContext.SubmitChanges();
- }
- RetrieveUserDetails();
- }
-
- protected void btnDelete_Click(object sender, EventArgs e)
- {
- using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
- {
- UserDetail Udetails = dtContext.UserDetails.SingleOrDefault(x => x.UserId == 5);
- dtContext.UserDetails.DeleteOnSubmit(Udetails);
- dtContext.SubmitChanges();
- }
- RetrieveUserDetails();
-
- }
- }
- }
Use this snippet to perform CRUD.
Closure
In this article we learned how we can perform CRUD using LINQ to SQL. I hope you liked this. Comments and complements are always welcomed.