Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.


Edit, Update, Delete and Insert data with DataList control using LINQ.
Often we will get requirements to perform edit, update, delete and insert data displayed using databound controls. Traditionally, we have been doing this using SQL procedure or SQL query with the support of the inbuilt events like edit, update and delete on the databound controls. With the introduction of LINQ, it is now possible to interact with the database using a new querying capability that has .net language syntax called LINQ query. This is done with the help of LINQ to SQL or LINQ to Entities classes(ORM model) which actually converts the LINQ query to SQL query and fire it on the database.
Moving forward, we will build edit, update, delete and insert feature for DataList control using LINQ. Throughout this article, we will construct our own LINQ query to insert, update, delete and select operations. We will build a sample application which binds all employee information present in the Sql Express database.
Steps
1:Open Visual Studio 2008.
2.Click New Website and Select “ASP.Net Web Site”. You can select the language of your choice. I have selected C#. Rename the website name as per your need.
3.Include a new SQL express database inside App_Data folder and create a table called Employee.
You can add database by right clicking App_Data folder in the solution and clicking Add New Item. This will bring a dialog box where you need to select “Sql Server Database” and click Add. Then, create a table called Employee with the necessary columns using the “Server Explorer”. Just right click the added database and click “Open” to open your database using Server Explorer on the left pane of your Visual Studio 2008. Refer my Server Explorer.
4:Next, we will build the LINQ to SQL class for our Employee table to do our database operations. Right click your project in the solution explorer and select “Add New Item” option. Select LINQ to SQL class, rename it if required and click Add. I have named it as EmployeesDataClasses.dbml.
5:From the server explorer, just drag and drop the Employees table into the LINQ to SQL designer to create the DataContext class and other necessary classes required for the database operations. The LINQ query will use these DataContext class to interact with the database. Thus, we have our LINQ to SQL class ready and we will start building the DataList control in the coming sections.
Edit Update in DataList Control
In order to perform edit operation with DataList control, we need to specify the EditItemTemplate which can populate the editing row data in input controls like TextBox, DropDownList, etc.Refer the below code,
<EditItemTemplate>
<table>
<tr>
<td width="100px">
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td width="20px">
<asp:Label ID="EmpIDLabel1" runat="server" Text='<%# Eval("EmployeeID") %>' />
</td>
<td width="150px">
<asp:TextBox ID="EmpNameTextBox" Width="100px" runat="server" Text='<%# Bind("EmployeeName") %>' />
</td>
<td width="80px">
<asp:TextBox ID="txtDept" runat="server" Width="80px" Text='<%# Bind("Department") %>' />
</td>
<td width="100px">
<asp:TextBox ID="txtAddress" runat="server" Width="100px" Text='<%# Bind("Address") %>' />
</td>
<td width="100px">
<asp:TextBox ID="txtCity" runat="server" Width="100px" Text='<%# Bind("City") %>' />
</td>
<td width="100px">
<asp:TextBox ID="txtState" runat="server" Width="100px" Text='<%# Bind("State") %>' />
</td>
<td width="100px">
<asp:TextBox ID="txtCountry" runat="server" Width="100px" Text='<%# Bind("Country") %>' />
</td>
</tr>
</table>
</EditItemTemplate>
Next, when the user clicks Edit button we need to set the DataList control’s EditItemIndex property with the editing row index to populate the data in the textboxes. This should be done on oneditcommand event.
The mark up is shown below:
protected void dlEmployee_EditCommand(object source,DataListCommandEventArgs e)
{
dlEmployee.EditItemIndex = e.Item.ItemIndex;
BindEmp();
}
public void BindEmp()
{       
  EmployeesDataClassesDataContext dbEmp = new EmployeesDataClassesDataContext()
  var LINQQuery = from emp in dbEmp.Employees                        
                               select emp;   
  dlEmployee.DataSource = LINQQuery       
  dlEmployee.DataBind();
}
 
 
 
       



Job or extra money for students

Search Engine Rank of your blog or websites