This tutorial will show how we can use LINQ to update records in a database, using a GridView Control. This examples was written in Visual Studio .NET 2008
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Looking for more .NET Database Tutorials? Click Here!
One of Microsoft's new additions to the .NET Framework 3.5 is LINQ. It is supposed to create a unified way of communicating with datasources - be it SQL databases, XML, controls - whatever. And it works very well. In this tutorial, we will be looking at how we can use LINQ to display and update a database using a GridView Control. The example included was written with Visual Studio .NET 2008, but can be followed with 2005 providing the LINQ Preview is installed. This can be downloaded for free from the Microsoft website.
We will start by creating a sample database - for the example, we created one table for people, with columns for their name and city. Once we have our sample database - or you have an existing database you wish to use, we can move onto creating the SQL Classes.
In solution explorer, we can right-click our project and then choose Add New Item. We will want to choose LINQ to SQL Classes. This will bring up a design view for us to build our database structure. This is so that LINQ can build a class for tables, relationships, stored procedures, etc. that we want to work with. We can goto the Server Explorer and simply drag into the design area the tables we want to work with. Once we are done, make sure you save the design.
In the toolbox, under data, you should notice a LinqDataSource. This works in a similar way to the SqlDataSource. We can drag this onto our ASPX page, or simply type something like the following out:
| <asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EnableUpdate="True" TableName="tblPeoples"> </asp:LinqDataSource> |
Take note of the EnableUpdate attribute of the LinqDataSource. This can be toggled on and off in the Smart Tag menu in the design view. Having this enabled will allow us to commit updates to the database. To display the data from the database and also to enable updating, we are going to use a GridView Control. This gives us an extremely easy way of allowing the user to update data quickly and easily. Go ahead and drag onto our ASPX page a GridView Control, or type the following:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="LinqDataSource1" Width="547px"> <Columns>
<asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" /> </Columns> </asp:GridView> |
Notice that the GridView has its AutoGenerateColumns attribute to false, and the DataSourceID is set to that of the LinqDataSource we created earlier. We also have the ShowEditButton set to True to allow the user to edit each record that is displayed. We can change this in the Smart Tag menu in Design View. If we run this right now, the GridView should display the data straight from the database, with each row having its own Edit link, which allows the user to click and then allow the editing of each column in the database for that record (with the exception of the Primary Key).
The entire ASPX page will look something like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">
<title>Using LINQ and GridView to Update Database in ASP.NET 3.5</title> </head>
<body>
<form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="LinqDataSource1" Width="547px"> <Columns>
<asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" /> </Columns> </asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EnableUpdate="True" TableName="tblPeoples"> </asp:LinqDataSource> </form> </body> </html> |
Looking for more .NET Database Tutorials? Click Here!
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!