CRUD Operations Using LINQ to SQL In ASP.NET #mesameergaikwad

 

CRUD Operations Using LINQ to SQL In ASP.NET

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 the project and Add New Item, then Data and select LINQ to SQL 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 a local database (Windows Authentication).

Step 6: Test Connection.

Step 7: The selected database will be displayed in Server Explorer.

Step 8: You can see the table under the selected database.

Step 9: Now drag and drop that table over DataClasses1.dbml. Here table name is converted into a typical C# class and its attributes converted into properties.

Step 10: Now add a web form 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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace WebApplication1
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. private void RetrieveUserDetails()
  15. {
  16. DataClasses1DataContext dtContext = new DataClasses1DataContext();
  17. GridView1.DataSource = dtContext.UserDetails;
  18. GridView1.DataBind();
  19. }
  20. protected void btnSelect_Click(object sender, EventArgs e)
  21. {
  22. RetrieveUserDetails();
  23. }
  24. protected void btnInsert_Click(object sender, EventArgs e)
  25. {
  26. using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
  27. {
  28. UserDetail Udetails = new UserDetail {
  29. UserName=”SAMEER”,
  30. City=”MUMBAI”,
  31. Designation=”SE”
  32. };
  33. dtContext.UserDetails.InsertOnSubmit(Udetails);
  34. dtContext.SubmitChanges();
  35. }
  36. RetrieveUserDetails();
  37. }
  38. protected void btnUpdate_Click(object sender, EventArgs e)
  39. {
  40. using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
  41. {
  42. UserDetail Udetails = dtContext.UserDetails.SingleOrDefault(x => x.UserId == 6);
  43. Udetails.Designation = “Leader”;
  44. dtContext.SubmitChanges();
  45. }
  46. RetrieveUserDetails();
  47. }
  48. protected void btnDelete_Click(object sender, EventArgs e)
  49. {
  50. using (DataClasses1DataContext dtContext = new DataClasses1DataContext())
  51. {
  52. UserDetail Udetails = dtContext.UserDetails.SingleOrDefault(x => x.UserId == 5);
  53. dtContext.UserDetails.DeleteOnSubmit(Udetails);
  54. dtContext.SubmitChanges();
  55. }
  56. RetrieveUserDetails();
  57. }
  58. }
  59. }

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 compliments are always welcomed.

Post a Comment

0 Comments