Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Using LINQ to Display Data in ASP.NET Web Application
Using LINQ to Display Data in ASP.NET Web Application


ASP.NET Database Tutorial

This tutorial will give you an introduction to using Microsoft's new LINQ to display data from a database using a DataGrid Control. This example is done using VS 2008, but LINQ can be used in 2005 by downloading the Preview from Microsoft.

This tutorial was created with Visual Studio 2008. If you are using 2005, Microsoft's LINQ Community Technology Preview release can be downloaded here.
If you have Visual Studio.NET 2008, then LINQ capabilities are built-in.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

When we open Visual Studio after installing the LINQ Preview, we will notice a new Project Template - LINQ ASP.NET Web Site Template. This is what we will be working on in this tutorial. If we open up the code-behind, we will notice a few new namespace references, also:

Visual Studio 2005 with LINQ Release installed:

using System;
using System.Data;
using System.Data.DLinq;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Query;

Visual Studio 2008:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

LINQ will make a uniform way for us to communicate with our data. We will start by creating a database - right-click on your project in the Solution Explorer and choose Add New Item. We want a SQL Database. Press Yes if it asks if you want to put it in the App_Data folder. We will add a new table in the Server Explorer, and add create three columns - id, name and city. The id column will be the primary key and identifier. We will name this table Table1.

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!

We can close that for now and then re-open it in data view so we can add some sample data (right-click the table in Server Explorer and choose to Show Table Data).
Once a few rows have been added, we can close the table. Now we want to add the LINQ to SQL Classes. Right-click your project in Solution Explorer and choose Add New Item again. You should have the option to add LINQ to SQL Classes. Choose this and if it asks you to put in the App_Code folder, choose yes. At this point you should be looking at a blank design space for our LINQ DataClasses. What we want to do is drag from the Server Explorer, the tables we want to work with. In this example, we are just going to work with the one table we just created. Once we drag it onto the design area, the table is represented graphically. When we are done, we can close this design view - making sure we save what we have changed.

Next, we want to add a LinqDataSource Control to our ASPX page, so drag one from the Data toolbox. We should have something like the following:

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

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Next, we can click the Smart Tag of the DataSource in the design view and choose to Configure Data Source. We choose from the DropDown the Data Context we just created, and then move on to select the data we want to work with. When we are done, we click Finish. Notice now that we get the option to Enable Delete, Insert and Update in the Smart Tag for the Data Source. This is similar to a SqlDataSource, but LINQ creates all the methods for us to interact with our data. In this example, we are just going to display data, so we'll go ahead and add a DataGrid to display the data. We can then also set the Data Source of the DataGrid to the one we just created. Our page will then look something like this:

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

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Now when we run this, we will see that the DataGrid displays all records from the database, and we haven't written one line of code. The power of Linq is displayed, but it can do much much more.

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

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks