asp:Feature

LANGUAGES: C#

ASP.NET VERSIONS: 2.0

 

Understanding the LinqDataSource Control

Execute LINQ Queries Declaratively to Bind Data to ASP.NET Data Controls

 

By Joydip Kanjilal

 

The newly added data source controls of ASP.NET 2.0 and beyond are great in the sense that you can implement CRUD (Create, Read, Update, and Delete) operations in your applications without having to write much code. Language Integrated Query, or LINQ as it is called, ships with the Microsoft .NET Framework and provides an easy way to access data, execute queries, and model your databases using .NET objects. According to MSDN, Language-Integrated Query (LINQ) is a set of features in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store.

 

This article explains the LinqDataSource Control that ships with ASP.NET 2.0 and beyond, and implements the DataSourceControl pattern of ASP.NET 2.0 and how it can be used for declarative binding of ASP.NET data controls.

 

The New Data Source Controls of ASP.NET 2.0

ASP.NET 2.0 ships with a lot of data source controls, namely:

  • ObjectDataSource
  • AccessDataSource
  • SQLDataSource
  • XMLDataSource
  • LinqDataSource

 

You use the ObjectDataSource control to bind data to the data controls from generic business objects. The AccessDataSource, SQLDataSource, and XMLDataSource controls are used to bind data to the ASP.NET data controls from Access, SQL Server Database, or XML data sources, respectively.

 

Similar to the other data source controls, the LinqDataSource control can be used for declarative data binding, i.e., you can declaratively bind data to your ASP.NET Data Controls in your Web page. Using the LinqDataSource control, you can eliminate data-store-specific coding in your applications, as this control can be used to bind data to your ASP.NET data controls from any LINQ-enabled data model. You can even execute stored procedures and complex queries using this control. Scott Guthrie says in his blog, One of the benefits of using the <asp:linqdatasource> control is that it leverages the flexibility that LINQ based ORMs provide. You don t need to define custom query/insert/update/delete methods for the data source to call - instead you can point the <asp:linqdatasource> control at your data model, identify what entity table you want it to work against, and then bind any ASP.NET UI control against the <asp:linqdatasource> and have them work with it.

 

The LinqDataSource control is a good fit in applications where you need to perform CRUD operations by leveraging the advantage of the unified programming model that LINQ provides. You can find more detailed discussion on these data source controls in my book, ASP.NET Data Presentation Controls Essentials (Packt Publishing).

 

Using the LinqDataSource Control

To get started with this control, you can simply drag and drop it from the toolbox onto your Web form. The LinqDataSource control is very flexible in the sense that you can use it to bind data to a data control from a wide variety of data sources. Note that the LinqDataSource control will not connect to your database directly. Instead, it will interact with entity classes you need to generate using the Object Relational Designer or the SqlMetal.exe tools.

 

The ContextTypeName property of the LinqDataSource control is used to map it to the entity class that represents the database. The TableName property is used to map the control to the class that represents the database table in use. Here s how the markup code of a typical LinqDataSource control looks:

 

<asp:LinqDataSource

   runat="server"

   ContextTypeName="CustomerDataContext"

   TableName="Customer"

   ID=" myLinqDataSource">

</asp:LinqDataSource>

 

You can then create a data source control in your application and associate the DataSourceID property of it to the LinqDataSource control, as shown here:

 

<asp:GridView

   ID="GridView1"

   runat="server"

   DataSourceID=" myLinqDataSource" >

</asp:GridView>

 

You also can use bound fields of the GridView control to show data for only the columns of the Customer table you want. To do this, disable the AutoGenerateColumns attribute of the GridView control.

 

Here s the complete markup code for both controls:

 

<asp:LinqDataSource

   ContextTypeName="CustomerDataContext"

   TableName="Customer"

   ID="myLinqDataSource"

   runat="server">

</asp:LinqDataSource>

<asp:GridView

   DataSourceID="myLinqDataSource"

   AutoGenerateColumns="false"

   ID="GridView1"

   runat="server">

   <Columns>

       <asp:BoundField DataField="Customer_Code" />

       <asp:BoundField DataField="Customer_Name" />

       <asp:BoundField DataField="Customer_Address" />

       <asp:BoundField DataField="Customer_Zip" />

 <asp:BoundField DataField="Customer_Phone" />

   </Columns>

</asp:GridView>

 

Conclusion

This article took a look at the LinqDataSource control and how it can be used to declaratively bind data to the ASP.NET data controls. Please send me your comments. Happy reading!

 

Joydip Kanjilal is a Microsoft MVP in ASP.NET. He is the author of ASP.NET Data Presentation Controls Essentials (Packt Publishing; http://www.packtpub.com/asp-net-data-presentation-controls/book). He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies. He has authored articles for some of the most reputable sites, like http://www.asptoday.com, http://www.devx.com, http://www.aspalliance.com, http://www.aspnetpro.com, http://www.sql-server-performance.com, http://www.sswug.com, etc. Many of these articles have been selected at http://www.asp.net (Microsoft s Official Site on ASP.NET). Joydip also was a community credit winner at http://www.community-credit.com a number of times. He is currently working as a Senior Consultant in a reputable company in Hyderabad, India. He has years of experience in designing and architecting solutions for various domains. His technical strengths include C, C++, VC++, Java, C#, Microsoft .NET, AJAX, Design Patterns, SQL Server, Operating Systems, and Computer Architecture. Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books, blogging, and writing books and articles. His hobbies include watching cricket and soccer, and playing chess. Contact Joydip via e-mail at mailto:joydipkanjilal@yahoo.com and view his MVP profile at https://mvp.support.microsoft.com/default.aspx/profile/joydip.