LINQ To SQL - CRUD Operations

We have seen a lot of articles on LINQ to SQL. Before going through this article, I would recommend to have some basic information about LINQ to SQL. Refer to these links for the same.

So, what is LINQ?

LINQ stands for Language Integrated Query. LINQ enables us to query any type of data store (SQL Server documents, objects in memory etc.).

I will not cover the details here in this article. You can read the complete article from the links mentioned above.

Let’s get started!!

We want to achieve this output. 


We will create a simple example which is LINQ to SQL for CRUD operations.

First, create an empty application and give it a suitable name.

Now, let’s create a sample table called registration. Here is the script to create a table.

  1. USE [TEST]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[registration]    Script Date: 1/16/2017 11:53:27 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. CREATE TABLE [dbo].[registration](  
  12.     [id] [int] IDENTITY(1,1) NOT NULL,  
  13.     [FirstName] [nvarchar](maxNULL,  
  14.     [MiddleName] [nvarchar](maxNULL,  
  15.     [LastName] [nvarchar](maxNULL,  
  16.     [Address] [nvarchar](maxNULL,  
  17.     [Landmark] [nvarchar](maxNULL,  
  18.     [EmailID] [nvarchar](maxNULL,  
  19.     [Country] [nvarchar](maxNULL,  
  20.     [State] [nvarchar](maxNULL,  
  21.     [City] [nvarchar](maxNULL,  
  22.     [MobileNumber] [nvarchar](maxNULL,  
  23.  CONSTRAINT [PK_registration] PRIMARY KEY CLUSTERED   
  24. (  
  25.     [id] ASC  
  26. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  27. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  28.   
  29. GO   

Adding DBML File

Now, let’s add LINQ to SQL Class file. To do that, click on the Project ->Add New Items -> Data Tab,
select LINQ to SQL Classes, name it as "Sample.dbml", and Click OK.

LINQ to SQL.

 
Now, open the Server Explorer in Visual Studio and locate the database that you created. After that, search for the table registration. Drag and drop the table on LINQ to SQL designer class.

When you drag and drop the table on LINQ to SQL designer, it will automatically create a business object for us. Here, sampledatacontext is our main file and the entry point or gateway of our application. The operations which we are going to perform like insert, update, and delete, will happen through this file. The Sampledatacontext will initiate the connection strings for us in web.config file.

So, the connection string here is.


GUI Part

Add a webform. Now, let’s setup the GUI part. First, we will include all the support files like bootstrap and jQuery.

  1. <link href="Styles/bootstrap.css" rel="stylesheet" />  
  2.    <script src="Scripts/jquery-1.8.2.js"></script>  
  3.    <script src="Scripts/bootstrap.js"></script>    

These are supporting files for our application. We will be including a modal popup here. So first, drag and drop the Script Manager control on the form.

  1. <asp:ScriptManager runat="server" ID="ScriptManager1" />   

Select Code

Now, let’s place the Grid View inside the UpdatePanel control.

  1. <!-- Placing GridView in UpdatePanel-->  
  2.             <asp:UpdatePanel ID="upCrudGrid" runat="server">  
  3.                 <ContentTemplate>  
  4.                             
  5.                     <asp:GridView ID="GridView1" runat="server" Width="940px" HorizontalAlign="Center"  
  6.                         OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false" AllowPaging="true"  
  7.                         CssClass="table table-hover table-striped">  
  8.                         <Columns>  
  9.                             
  10.                               
  11.                             <asp:BoundField DataField="id" HeaderText="ID"/>  
  12.                             <asp:BoundField DataField="FirstName" HeaderText="FirstName"/>  
  13.                                   <asp:BoundField DataField="MiddleName" HeaderText="MiddleName"/>  
  14.                             <asp:BoundField DataField="LastName" HeaderText="LastName"/>  
  15.                             <asp:BoundField DataField="Address" HeaderText="Address"/>  
  16.                             <asp:BoundField DataField="Landmark" HeaderText="Landmark"/>  
  17.                             <asp:BoundField DataField="EmailID" HeaderText="Email"/>  
  18.                              <asp:BoundField DataField="Country" HeaderText="Country"/>  
  19.                              <asp:BoundField DataField="State" HeaderText="State"/>  
  20.                              <asp:BoundField DataField="City" HeaderText="City"/>  
  21.                              <asp:BoundField DataField="MobileNumber" HeaderText="MobileNo"/>  
  22.   
  23.                           
  24.                                       
  25.                          <asp:ButtonField CommandName="editRecord"    ControlStyle-CssClass="btn btn-info"  
  26.                                 ButtonType="Button" Text="Edit" HeaderText="Edit Record" >  
  27.                                 <ControlStyle CssClass="btn btn-primary" ></ControlStyle>  
  28.                             </asp:ButtonField>  
  29.                             <asp:ButtonField CommandName="deleteRecord" ControlStyle-CssClass="btn btn-info"  
  30.                                 ButtonType="Button" Text="Delete" HeaderText="Delete Record">  
  31.                                 <ControlStyle CssClass="btn btn-danger"></ControlStyle>  
  32.                             </asp:ButtonField>  
  33.                         </Columns>  
  34.                     </asp:GridView>  
  35.                     <asp:Button ID="btnAdd" runat="server" Text="Add New Record" CssClass="btn btn-info" OnClick="btnAdd_Click" />  
  36.                 </ContentTemplate>  
  37.                 <Triggers>  
  38.                 </Triggers>  
  39.             </asp:UpdatePanel>   

Here, in this code, we are selecting records from our database along with buttons for edit and delete; and at the bottom, we are adding a button for adding a new record.

After setting up the GUI for "Select", let’s create a simple method for "Select" using LINQ to SQL.

  1. private void BindEmployee()  
  2.        {  
  3.            SampleDataContext dbcontext = new SampleDataContext();  
  4.            GridView1.DataSource = from reg in dbcontext.registrations  
  5.                                   select reg;  
  6.            GridView1.DataBind();  
  7.        }   

Create a method as BindEmployee(), create an instance of your Sampledatacontext class, and bind the Grid View with a LINQ query.

So, it’s a simple straight forward code. Now, let’s run the application and check the output.

Output for Select


Insert Code

Now, let’s setup the GUI part for insert.

  1. <!-- Add Record Modal Starts here-->  
  2.             <div id="addModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">  
  3.                 <div class="modal-header">  
  4.                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  5.                     <h3 id="addModalLabel">Add New Record</h3>  
  6.                 </div>  
  7.                 <asp:UpdatePanel ID="upAdd" runat="server">  
  8.                     <ContentTemplate>  
  9.                         <div class="modal-body">  
  10.                             <table class="table table-bordered table-hover">  
  11.   
  12.                                   <tr>  
  13.                                     <td>ID : </td>  
  14.                                     <td>  
  15.                                         <asp:TextBox ID="txtIdAdd" runat="server"></asp:TextBox></td>  
  16.                                 </tr>  
  17.                                   <tr>  
  18.                                     <td>FirstName : </td>  
  19.                                     <td>  
  20.                                         <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>  
  21.                                 </tr>  
  22.                                 <tr>  
  23.                                     <td>MiddleName :</td>  
  24.                                     <td>  
  25.                                         <asp:TextBox ID="txtMiddleName" runat="server"></asp:TextBox></td>  
  26.                                 </tr>  
  27.                                 <tr>  
  28.                                     <td>LastName:</td>  
  29.                                     <td>  
  30.                                         <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>  
  31.                                 </tr>  
  32.                                 <tr>  
  33.                                     <td>Address:</td>  
  34.                                     <td>  
  35.                                         <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>  
  36.                                 </tr>  
  37.                                      <tr>  
  38.                                     <td>Landmark:</td>  
  39.                                     <td>  
  40.                                         <asp:TextBox ID="txtLandmark" runat="server"></asp:TextBox></td>  
  41.                                 </tr>  
  42.                                      <tr>  
  43.                                     <td>EmailID:</td>  
  44.                                     <td>  
  45.                                         <asp:TextBox ID="txtEmailID" runat="server"></asp:TextBox></td>  
  46.                                 </tr>  
  47.                                      <tr>  
  48.                                     <td>Country:</td>  
  49.                                     <td>  
  50.                                         <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox></td>  
  51.                                 </tr>  
  52.                                      <tr>  
  53.                                     <td>State:</td>  
  54.                                     <td>  
  55.                                         <asp:TextBox ID="txtState" runat="server"></asp:TextBox></td>  
  56.                                 </tr>  
  57.                                      <tr>  
  58.                                     <td>City:</td>  
  59.                                     <td>  
  60.                                         <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>  
  61.                                 </tr>  
  62.                                      <tr>  
  63.                                     <td>Mobilenumber:</td>  
  64.                                     <td>  
  65.                                         <asp:TextBox ID="txtMobileNumber" runat="server"></asp:TextBox></td>  
  66.                                 </tr>  
  67.                             </table>  
  68.                         </div>  
  69.                         <div class="modal-footer">                            
  70.                             <asp:Button ID="btnAddRecord" runat="server" Text="Add" CssClass="btn btn-primary" OnClick="btnAddRecord_Click" />  
  71.                             <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>  
  72.                         </div>  
  73.                     </ContentTemplate>  
  74.                     <Triggers>  
  75.                         <asp:AsyncPostBackTrigger ControlID="btnAddRecord" EventName="Click" />  
  76.                     </Triggers>  
  77.                 </asp:UpdatePanel>  
  78.             </div>   

Here, we have placed the respective textboxes on the modal popup and called the modal popup on button click in code behind.

We will see how to pop the modal up through code behind.

  1.      protected void btnAddRecord_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.                 System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  5.                 sb.Append(@"<script type='text/javascript'>");  
  6.                 sb.Append("alert('Record Added Successfully');");  
  7.                 sb.Append("$('#addModal').modal('hide');");  
  8.                 sb.Append(@"</script>");  
  9.                 ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "AddHideModalScript", sb.ToString(), false);  
  10.            
  11. }   

So, this is the modal popup on "Add New Record" button. Let’s run the application and check.

So, we have created the modal pop up successfully. Now, let’s write the code for inserting.

  1. protected void btnAddRecord_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.             using (SampleDataContext dbcontext = new SampleDataContext())  
  5.             {  
  6.                 registration reg = new registration();  
  7.                 reg.id = Convert.ToInt32(txtIdAdd.Text);  
  8.                 reg.FirstName = txtFirstName.Text;  
  9.                 reg.MiddleName = txtMiddleName.Text;  
  10.                 reg.LastName = txtLastName.Text;  
  11.                 reg.Address = txtAddress.Text;  
  12.                 reg.Landmark = txtLandmark.Text;  
  13.                 reg.EmailID = txtEmailID.Text;  
  14.                 reg.Country = txtCountry.Text;  
  15.                 reg.State = txtState.Text;  
  16.                 reg.City = txtCity.Text;  
  17.                 reg.MobileNumber = txtMobileNumber.Text;  
  18.                 dbcontext.registrations.InsertOnSubmit(reg);  
  19.                 dbcontext.SubmitChanges();  
  20.   
  21.   
  22.                 System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  23.                 sb.Append(@"<script type='text/javascript'>");  
  24.                 sb.Append("alert('Record Added Successfully');");  
  25.                 sb.Append("$('#addModal').modal('hide');");  
  26.                 sb.Append(@"</script>");  
  27.                 ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "AddHideModalScript", sb.ToString(), false);  
  28.            
  29.                 BindEmployee();  
  30.                // clearfields();  
  31.             }  
  32.         }   

We are wrapping sampledatacontext class inside using block, just to make sure that we are opening and closing our connection respectively. After that, we create a variable reg and pass single records in our registration table, which are defined in our Sampledatacontext class. This will pass the values which are filled with properties i.e. textboxes here. Last, we are calling submitchanges() in order to submit those details in our database, and a popup to show that the records are inserted successfully.

Performing Edit

On button click (Edit), we want to achieve loading of the details of that employee.

Let’s setup the GUI part for editing.

  1. <div id="editModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">  
  2.                 <div class="modal-header">  
  3.                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  4.                     <h3 id="editModalLabel">Edit Record</h3>  
  5.                 </div>  
  6.                 <asp:UpdatePanel ID="upEdit" runat="server">  
  7.                     <ContentTemplate>  
  8.                         <div class="modal-body">  
  9.                             <table class="table">  
  10.                                  
  11.                              <tr>  
  12.                                     <td>ID : </td>  
  13.                                     <td>  
  14.                                         <asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>  
  15.                                 </tr>  
  16.                                 <tr>  
  17.                                     <td>FirstName : </td>  
  18.                                     <td>  
  19.                                         <asp:TextBox ID="txtFirstNameEdit" runat="server"></asp:TextBox></td>  
  20.                                 </tr>  
  21.                                 <tr>  
  22.                                     <td>MiddleName :</td>  
  23.                                     <td>  
  24.                                         <asp:TextBox ID="txtMiddleNameEdit" runat="server"></asp:TextBox></td>  
  25.                                 </tr>  
  26.                                 <tr>  
  27.                                     <td>LastName:</td>  
  28.                                     <td>  
  29.                                         <asp:TextBox ID="txtLastNameEdit" runat="server"></asp:TextBox></td>  
  30.                                 </tr>  
  31.                                 <tr>  
  32.                                     <td>Address:</td>  
  33.                                     <td>  
  34.                                         <asp:TextBox ID="txtAddressEdit" runat="server"></asp:TextBox></td>  
  35.                                 </tr>  
  36.                                      <tr>  
  37.                                     <td>Landmark:</td>  
  38.                                     <td>  
  39.                                         <asp:TextBox ID="txtLandmarkEdit" runat="server"></asp:TextBox></td>  
  40.                                 </tr>  
  41.                                      <tr>  
  42.                                     <td>EmailID:</td>  
  43.                                     <td>  
  44.                                         <asp:TextBox ID="txtEmailIDEdit" runat="server"></asp:TextBox></td>  
  45.                                 </tr>  
  46.                                      <tr>  
  47.                                     <td>Country:</td>  
  48.                                     <td>  
  49.                                         <asp:TextBox ID="txtCountryEdit" runat="server"></asp:TextBox></td>  
  50.                                 </tr>  
  51.                                      <tr>  
  52.                                     <td>State:</td>  
  53.                                     <td>  
  54.                                         <asp:TextBox ID="txtStateEdit" runat="server"></asp:TextBox></td>  
  55.                                 </tr>  
  56.                                      <tr>  
  57.                                     <td>City:</td>  
  58.                                     <td>  
  59.                                         <asp:TextBox ID="txtCityEdit" runat="server"></asp:TextBox></td>  
  60.                                 </tr>  
  61.                                      <tr>  
  62.                                     <td>Mobilenumber:</td>  
  63.                                     <td>  
  64.                                         <asp:TextBox ID="txtMobileNumberEdit" runat="server"></asp:TextBox></td>  
  65.                                 </tr>  
  66.                             </table>  
  67.                         </div>  
  68.                         <div class="modal-footer">  
  69.                             <asp:Label ID="lblResult" Visible="false" runat="server"></asp:Label>  
  70.                             <asp:Button ID="btnSave" runat="server" Text="Update" CssClass="btn btn-info" OnClick="btnSave_Click" />  
  71.                             <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>  
  72.                         </div>  
  73.                     </ContentTemplate>  
  74.                     <Triggers>  
  75.                         <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />  
  76.                         <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />  
  77.                     </Triggers>  
  78.                 </asp:UpdatePanel>  
  79.             </div>  
  80.             <!-- Edit Modal Ends here -->   

Let's write the code for Edit.

  1. if (e.CommandName == "editRecord")  
  2.             {  
  3.                
  4.                 int index = Convert.ToInt32(e.CommandArgument);  
  5.   
  6.                 GridViewRow gr = GridView1.Rows[index];  
  7.                 txtID.Text = gr.Cells[0].Text;  
  8.                 txtFirstNameEdit.Text = gr.Cells[1].Text;  
  9.                 txtMiddleNameEdit.Text = gr.Cells[2].Text;  
  10.                 txtLastNameEdit.Text = gr.Cells[3].Text;  
  11.                 txtAddressEdit.Text = gr.Cells[4].Text;  
  12.                 txtLandmarkEdit.Text = gr.Cells[5].Text;  
  13.                 txtEmailIDEdit.Text = gr.Cells[6].Text;  
  14.                 txtCountryEdit.Text = gr.Cells[7].Text;  
  15.                 txtStateEdit.Text = gr.Cells[8].Text;  
  16.                 txtCityEdit.Text = gr.Cells[9].Text;  
  17.                 txtMobileNumberEdit.Text = gr.Cells[10].Text;  
  18.                ViewState["id"]= txtID.Text;  
  19.   
  20.   
  21.   
  22.   
  23.                 System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  24.                 sb.Append(@"<script type='text/javascript'>");  
  25.                 sb.Append("$('#editModal').modal('show');");  
  26.                 sb.Append(@"</script>");  
  27.                 ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "EditModalScript", sb.ToString(), false);  
  28.   
  29.             }   

We are calling the command button for edit. Here, we are retrieving values from databases and allocating them to respective cells.

 Now, let's write the code for updating respective values 
  1. protected void btnSave_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.   
  5.             using (SampleDataContext dbcontext = new SampleDataContext())  
  6.             {  
  7.   
  8.                 {  
  9.                //      var reg = dbcontext.registrations.SingleOrDefault(x => x.id == 4);  
  10.                    var reg = dbcontext.registrations.Single(employee => employee.id == Convert.ToInt32(ViewState["id"].ToString()));  
  11.                     // string id = lblid.Text;  
  12.                     reg.id = Convert.ToInt32(txtID.Text);  
  13.                     reg.FirstName = txtFirstNameEdit.Text.ToString();  
  14.                     reg.MiddleName = txtMiddleNameEdit.Text.ToString();  
  15.                     reg.LastName = txtLastNameEdit.Text.ToString();  
  16.                     reg.Address = txtAddressEdit.Text.ToString();  
  17.                     reg.Landmark = txtLandmarkEdit.Text.ToString();  
  18.                     reg.Country = txtCountryEdit.Text.ToString();  
  19.                     reg.State = txtStateEdit.Text.ToString();  
  20.                     reg.City = txtCityEdit.Text.ToString();  
  21.                     reg.EmailID = txtEmailIDEdit.Text.ToString();  
  22.                     reg.MobileNumber = txtMobileNumberEdit.Text.ToString();  
  23.                 }  
  24.                 
  25.   
  26.             dbcontext.SubmitChanges();  
  27.             
  28.             BindEmployee();  
  29.              
  30.             BindEmployee();  
  31.             System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  32.             sb.Append(@"<script type='text/javascript'>");  
  33.             sb.Append("alert('Records Updated Successfully');");  
  34.             sb.Append("$('#editModal').modal('hide');");  
  35.             sb.Append(@"</script>");  
  36.             ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "EditHideModalScript", sb.ToString(), false);  
  37.             }  
  38.   
  39.         }   

So, we are wrapping sampledatacontext class inside, using block, just to make sure that we are opening and closing our connection respectively. After that, we create a variable reg and pass single records in our registration table which are defined in our Sampledatacontext class. This will pass the values which are filled with properties i.e. textboxes here. Last, we are calling submitchanges() in order to submit those details in our database, and a popup to show that the records are inserted successfully.

Delete Code

GUI Part.

  1. <!--Add Record Modal Ends here-->  
  2.             <!-- Delete Record Modal Starts here-->  
  3.             <div id="deleteModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="delModalLabel" aria-hidden="true">  
  4.                 <div class="modal-header">  
  5.                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  6.                     <h3 id="delModalLabel">Delete Record</h3>  
  7.                 </div>  
  8.                 <asp:UpdatePanel ID="upDel" runat="server">  
  9.                     <ContentTemplate>  
  10.                         <div class="modal-footer">  
  11.                             <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="btn btn-info" OnClick="btnDelete_Click" />  
  12.                             <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Cancel</button>  
  13.                         </div>  
  14.                     </ContentTemplate>  
  15.                     <Triggers>  
  16.                         <asp:AsyncPostBackTrigger ControlID="btnDelete" EventName="Click" />  
  17.                     </Triggers>  
  18.                 </asp:UpdatePanel>  
  19.             </div>  
  20.             <!--Delete Record Modal Ends here -->   

Code for delete 

  1. if (e.CommandName == "deleteRecord")  
  2.           {  
  3.   
  4.           int emp_ID = Convert.ToInt32(e.CommandArgument);  
  5.           SampleDataContext empDB = new SampleDataContext();  
  6.           var emp = empDB.registrations.Single(employee => employee.id == emp_ID);  
  7.           empDB.registrations.DeleteOnSubmit(emp);  
  8.           empDB.SubmitChanges();  
  9.           BindEmployee();  
  10.       }   

This is simple straightforward code. On Delete button, we are first capturing the ID of employee and then recreating the sampledatacontext object of our sample class. We are calling DeleteOnsubmit changes and deleting ID. Simple and straightforward code again.

Code for Edit and Delete 

  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2.         {  
  3.   
  4.             if (e.CommandName == "deleteRecord")  
  5.                 {  
  6.   
  7.                 int emp_ID = Convert.ToInt32(e.CommandArgument);  
  8.                 SampleDataContext empDB = new SampleDataContext();  
  9.                 var emp = empDB.registrations.Single(employee => employee.id == emp_ID);  
  10.                 empDB.registrations.DeleteOnSubmit(emp);  
  11.                 empDB.SubmitChanges();  
  12.                 BindEmployee();  
  13.             }  
  14.           else   if (e.CommandName == "editRecord")  
  15.             {  
  16.                
  17.                 int index = Convert.ToInt32(e.CommandArgument);  
  18.   
  19.                 GridViewRow gr = GridView1.Rows[index];  
  20.                 // string id = gr.Cells[0].Text;  
  21.                 // lblid.Text = gr.Cells[index].Text;  
  22.                 // int emp_ID = 0;  
  23.                 // lblid.Text = gr.Cells[0].Text;  
  24.                 //  txtID.Text = gr.Cells[0].Text;  
  25.                 txtID.Text = gr.Cells[0].Text;  
  26.                 txtFirstNameEdit.Text = gr.Cells[1].Text;  
  27.                 txtMiddleNameEdit.Text = gr.Cells[2].Text;  
  28.                 txtLastNameEdit.Text = gr.Cells[3].Text;  
  29.                 txtAddressEdit.Text = gr.Cells[4].Text;  
  30.                 txtLandmarkEdit.Text = gr.Cells[5].Text;  
  31.                 txtEmailIDEdit.Text = gr.Cells[6].Text;  
  32.                 txtCountryEdit.Text = gr.Cells[7].Text;  
  33.                 txtStateEdit.Text = gr.Cells[8].Text;  
  34.                 txtCityEdit.Text = gr.Cells[9].Text;  
  35.                 txtMobileNumberEdit.Text = gr.Cells[10].Text;  
  36.                ViewState["id"]= txtID.Text;  
  37.   
  38.   
  39.   
  40.   
  41.                 System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  42.                 sb.Append(@"<script type='text/javascript'>");  
  43.                 sb.Append("$('#editModal').modal('show');");  
  44.                 sb.Append(@"</script>");  
  45.                 ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "EditModalScript", sb.ToString(), false);  
  46.   
  47.             }  
  48.         }   

So, we are done now. Now, run the application and perform CRUD operation in LINQ to SQL.

Up Next
    Ebook Download
    View all
    Learn
    View all