Navigator: Home - Display - Displaying Database with LINQ and DataGrid ASP.NET 3.5

Displaying Database with LINQ and DataGrid ASP.NET 3.5

This tutorial will show how we can simply display the records of a database in a DataGrid Control with Microsoft's new technology, LINQ. This example was created in Visual Studio .NET 2008.

Download the Full Working Version of this Project written with Visual Studio.NET 2008 Here!

Looking for more .NET Database Tutorials? Click Here!

In this tutorial we will look at Microsoft's new baby, LINQ, and how we can use it to display data from a SQL Server Database. We are simply going to create a sample database and use a DataGrid to display the data with a LinqDataSource Control and LINQ to SQL Classes.

This example was created with Visual Studio .NET 2008. It can be recreated in 2005 with the LINQ Preview downloaded from Microsoft, but this tutorial is directed at Visual Studio .NET 2008.

The first thing we need to do is open up our project in Visual studio and create a SQL Server Database. We do this by right-clicking our project in Solution Explorer and then choosing Add New Item, SQL Server Database. For this example we will just be using a simple database with one table. We will have three columns - id, name, city. Once we have designed the database, we can add some sample data. If you already have a database you want to work with, let's move on to the next step.

We now want to add LINQ to SQL Classes to our project, because we are working with a SQL Database. We do this in a similar to way to as before, byt right-clicking our project and choosing Add New Item, LINQ to SQL Classes. This will open up a design view for our database. We will need to add all the tables and stored procedures we wish to work with. In this example, we just need to drag from the Server Explorer, our only table. Then we save when we are done.

Now we can start building our Web Application. We will add a LinqDataSource to our ASPX page from the Data Toolbox. It will look something like this:

<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>


Now we can add a DataGrid Control and associate the Data Source with the DataClasses we created just before. We can do this by typing directly into the code or using the Smart Tag. From the Smart Tag, we can choose Configure Data Source and then choose the Context Object, and then select the data we want to work with. The code will look something like this:

<form id="form1" runat="server">
<asp:DataGrid ID="DataGrid1" runat="server" DataSourceID="LinqDataSource1"
Width="265px"></asp:DataGrid>
<br />
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" TableName="tblPeoples">
</asp:LinqDataSource>
</form>


Above you can see that the DataGrid has its DataSource set as the LinqDataSource. This was done by simply clicking on the Smart Tag of the DataGrid and choosing the DataSource from there. Now if we run this, we will see the DataGrid display the database records, and we haven't even written one line of code.

Download the Full Working Version of this Project written with Visual Studio.NET 2008 Here!

Looking for more .NET Database Tutorials? Click Here!
411asp.net123aspxDotNetFreaksServer Intellect