This article is help to create new website having crud operation using linq to sql classes in asp net.It also explains step-by-step implementation of LINQ to SQL class for CRUD operations.
What Is LINQ To SQL?
Full meaning of LINQ is ‘Language Integrated Query’, which replaces the traditional sql query execution process. Moreover, it doesn’t only applicable to manipulate database results, but it can also be used to manipulates array/list collections. Linq was released as part of the .NET framework 3.0 and can be used from languages supported by .NET framework like C#, VB etc. The term ‘LINQ To SQL‘ refers to the technology by which we can use LINQ for access SQL Databases. Here in this tutorial, I will show step by step ways to get started with LINQ To SQL programming with C#.
Steps to implement LINQ to SQL:
Step 1: Create database “StudentDB”
Step 2: Create table Student
In student table set ‘StudentID’ as primary key
as in below screen shot:
Step 3:
Now start visual studio 2012
File->> New Website
Provide name of website set location and Click ‘OK’ as shown in below screen shot:
Step 4:
Right click on Website and Add new item select “LINQ to SQL Classes”
when we click “Add” button we will get message like below :
Click “Yes” , then see in your project one folder name “App_code” will be created with “DataClasses.dbml” file name
Step 5:
Open “DataClasses.dbml” file. Explore database “StudentDb” and select “Student” table and drag & drop it to “DataClasses.dbml” like shown below:
This will add a connection string in “Web.config” file
Step 6:
Right-click on the project file and select “Add New Item…”.
Select “Web Form” from the templates.
Give the name as “Default .aspx”.
Press the “Add” button.
This will add a Default.aspx page to your website’s root directory.
Step 7:
Now add the following code in your Default.aspx page under the “form” control:
<table border="1" style="width:500px"> <tr> <td> <asp:Label ID="lblmsg" runat="server"></asp:Label></td> </tr> <tr> <td>Name</td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td> </tr> <tr> <td>Contact</td> <td> <asp:TextBox ID="txtcontact" runat="server"></asp:TextBox></td> </tr> <tr> <td>Address</td> <td> <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td> </tr> <tr> <td>Email</td> <td> <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="btnInsert" runat="server" Text="Submit" OnClick="btnInsert_Click"/></td> <td><asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" Visible="false"/></td> <td><asp:HiddenField ID="hdnupdate" runat="server"/></td> </tr> </table> -----Gridview start--- <table border="1" style="width:500px"> <tr> <td> <asp:GridView ID="ShowData" runat="server" AutoGenerateColumns="false" OnRowCommand="ShowData_RowCommand"> <Columns> <asp:TemplateField HeaderText="StudentID"> <ItemTemplate> <asp:Label ID="lblID" Text='<%# Eval("StudentID") %>' runat="server"></asp:Label> <asp:HiddenField ID="hregid" Value='<%# Eval("StudentID") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="lblname" Text='<%# Eval("Name") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Contact"> <ItemTemplate> <asp:Label ID="lblcontact" Text='<%# Eval("Contact") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:Label ID="lbladdress" Text='<%# Eval("Address") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label ID="lblemail" Text='<%# Eval("Email") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:LinkButton ID="lnkbtnedt" Text="Edit" runat="server" CommandName="EDT" CommandArgument='<%# Eval("StudentID") %>'/> <asp:LinkButton ID="lnkbtndlt" Text="Delete" runat="server" CommandName="Dlt" CommandArgument='<%# Eval("StudentID") %>' OnClientClick="javascript:return confirm('Are you sure want to delete?');" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> </table> ----Gridview end---
Step 8: Now add the following code to your Default.aspx.cs file:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page { DataClassesDataContext linqtosql = new DataClassesDataContext(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } protected void btnInsert_Click(object sender, EventArgs e) { Student s = new Student(); s.Name = txtname.Text; s.Contact = txtcontact.Text; s.Address = txtaddress.Text; s.Email = txtemail.Text; linqtosql.Students.InsertOnSubmit(s); linqtosql.SubmitChanges(); lblmsg.Text = "Data inserted successfully"; CearControl(); BindGrid(); } public void BindGrid() ----This method is for Select records---- { var ss = from s in linqtosql.Students select s; ShowData.DataSource = ss; ShowData.DataBind(); } public void CearControl() -----This method is for Clear controls--- { txtname.Text = ""; txtcontact.Text = ""; txtaddress.Text = ""; txtemail.Text = ""; } protected void ShowData_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.ToString() == "Dlt") { var singleStudent = linqtosql.Students.Single(student => student.StudentID == Convert.ToInt64(e.CommandArgument.ToString())); linqtosql.Students.DeleteOnSubmit(singleStudent); linqtosql.SubmitChanges(); BindGrid(); } else { btnInsert.Visible = false; btnupdate.Visible = true; var edtStudent = linqtosql.Students.Single(student => student.StudentID == Convert.ToInt64(e.CommandArgument.ToString())); txtname.Text = edtStudent.Name; txtcontact.Text = edtStudent.Contact; txtaddress.Text = edtStudent.Address; txtemail.Text = edtStudent.Email; hdnupdate.Value = e.CommandArgument.ToString(); } } protected void btnupdate_Click(object sender, EventArgs e) { var UpdateStudent = linqtosql.Students.Single(student => student.StudentID == Convert.ToInt64(hdnupdate.Value)); UpdateStudent.Name = txtname.Text; UpdateStudent.Contact = txtcontact.Text; UpdateStudent.Address = txtaddress.Text; UpdateStudent.Email = txtemail.Text; linqtosql.SubmitChanges(); BindGrid(); lblmsg.Text = "Record Updated Successfully"; } }
Step 9: Now run website
Download sample of Select,Insert, Update and Delete Functionality with LINQ to SQL in asp.net
Conclusion : Here I have explained Select,Insert, Update and Delete Functionality with LINQ to SQL in asp.net