Tutorial RSS
 
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.


If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


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>


If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!



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.



Looking for more .NET Database Tutorials? Click Here!

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

411asp.net123aspxDotNetFreaksServer Intellect