LinqDataSource in Action
Here's a simple example that illustrates the steps involved in using the LinqDataSource control to retrieve and display data from the Products
table in the sample AdventureWorks database that ships with SQL Server. To begin, create a new Visual C# web site named LinqDataSourceExample
. After creating the web site, right-click the project item in the Solution Explorer, and select Add → New Item from the menu. In the Add New Item dialog box, select the "LINQ to SQL Classes" template and name it AdventureWorksDatabase.dbml
|Author's Note: When you hit "Add" in the "Add New Item" dialog box, Visual Studio presents a prompt asking whether you want to place the new class in the App_Code folder. For the purposes of this article, answer "Yes" to the prompt. Answering "Yes" lets you reference the class from within the project's ASP.NET pages without having to qualify the name of the class by prefixing it with the namespace.
After you've created the AdventureWorksDatabase
LinqDataSource class, Visual Studio opens the Object Relational Designer. You'll see a design surface on which you can drag and drop tables and stored procedures from the Server Explorer to create corresponding entity classes. The designer surface contains two panes: an Entities pane and a Methods pane. As the name suggests, the Entities pane displays entity classes included in your model, and the Methods pane displays DataContext methods that are mapped to stored procedures and functions.
Now select View → Server Explorer from the menu, and add a new data connection to the AdventureWorks database. After that, drag and drop the Production.ProductSubcategory
tables onto the Object Relational Designer from the Tables node in Server Explorer. Your screen should look somewhat similar to Figure 1
|Figure 1. Object Relational Designer: The designer surface shows the Entities and Methods panes after dragging the Product and ProductSubcategory tables from Server Explorer.|
The act of dropping the database objects onto the design surface causes the Object Relational Designer to create standard .NET classes that reflect the dropped objects. To see the code, take a look at the file AdventureWorksDatabase.designer.cs
file after dropping the tables on the design surface. You'll see that Visual Studio has generated several classes, including one called AdventureWorksDatabaseDataContext that manages connections, and one class for each of the tables in the designer.
Now that you have created the entity classes, you can create an ASP.NET page and use the LinqDataSource control to consume those classes. Create a new ASP.NET page named SimpleLinqDataSourceExample.aspx
, and modify its code as follows:
<%@ Page Language="C#" %>
<title>Using LinqDataSource Control to
execute a simple LINQ Query</title>
<form id="form1" runat="server">
<asp:ListView DataSourceID="productSource" ID="ListView1"
Product Name: <b><%# Eval("Name") %> </b><br />
Product Number: <b><%# Eval("ProductNumber") %></b>
<br /><br />
<asp:LinqDataSource ID="productSource" runat="server"
|Figure 2. Bound Data: Here's the output produced by binding the ListView control to a LinqDataSource control that retrieves data from the Products table.|
In the above code, the LinqDataSource control has two important properties named ContextTypeName
. The ContextTypeName
property refers to the object that represents the database (the LinqDataSource you created in the Object Relational Designer), and the TableName
property refers to the object that represents the database table.
The ListView control's DataSourceID
property is set to the name of the LinqDataSource control (productSource
). In the ListView's <ItemTemplate>
you use the Eval
expression to display the Name
property values of the Product class.
shows the output produced by navigating to the page in the browser.