Understanding the LinqDataSource Control

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

Joydip Kanjilal

October 30, 2009

4 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:Feature

LANGUAGES:C#

ASP.NETVERSIONS: 2.0

 

Understanding the LinqDataSource Control

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

 

By Joydip Kanjilal

 

The newly added data source controls of ASP.NET 2.0 andbeyond 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, andmodel your databases using .NET objects. According to MSDN, Language-Integrated Query (LINQ) is a set of features in Visual Studio 2008that extends powerful query capabilities to the language syntax of C# andVisual Basic. LINQ introduces standard, easily-learned patterns for queryingand updating data, and the technology can be extended to support potentiallyany kind of data store.

 

This article explains the LinqDataSource Control thatships with ASP.NET 2.0 and beyond, and implements the DataSourceControl patternof ASP.NET 2.0 and how it can be used for declarative binding of ASP.NET datacontrols.

 

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 thedata controls from generic business objects. The AccessDataSource,SQLDataSource, and XMLDataSource controls are used to bind data to the ASP.NETdata controls from Access, SQL Server Database, or XML data sources,respectively.

 

Similar to the other data source controls, the LinqDataSourcecontrol can be used for declarative data binding, i.e., you can declarativelybind data to your ASP.NET Data Controls in your Web page. Using the LinqDataSourcecontrol, you can eliminate data-store-specific coding in your applications, asthis control can be used to bind data to your ASP.NET data controls from any LINQ-enableddata model. You can even execute stored procedures and complex queries usingthis control. Scott Guthrie says in his blog, One of the benefits ofusing the control is that it leverages theflexibility that LINQ based ORMs provide. You don t need to define customquery/insert/update/delete methods for the data source to call - instead youcan point the control at your data model, identifywhat entity table you want it to work against, and then bind any ASP.NET UIcontrol against the and have them work with it.

 

The LinqDataSource control is a good fit in applicationswhere you need to perform CRUD operations by leveraging the advantage of theunified programming model that LINQ provides. You can find more detaileddiscussion 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 anddrop it from the toolbox onto your Web form. The LinqDataSource control is veryflexible in the sense that you can use it to bind data to a data control from awide variety of data sources. Note that the LinqDataSource control will notconnect to your database directly. Instead, it will interact with entityclasses you need to generate using the Object Relational Designer or theSqlMetal.exe tools.

 

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

 

   runat="server"    ContextTypeName="CustomerDataContext"    TableName="Customer"    ID=" myLinqDataSource">   You can then create a data source control in yourapplication and associate the DataSourceID property of it to the LinqDataSourcecontrol, as shown here:     ID="GridView1"    runat="server"    DataSourceID="myLinqDataSource" >   You also can use bound fields of the GridView control toshow 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:     ContextTypeName="CustomerDataContext"    TableName="Customer"    ID="myLinqDataSource"    runat="server">    DataSourceID="myLinqDataSource"    AutoGenerateColumns="false"    ID="GridView1"    runat="server">                                            Conclusion This article took a look at the LinqDataSource control andhow it can be used to declaratively bind data to the ASP.NET data controls.Please send me your comments. Happy reading!  Joydip Kanjilal isa Microsoft MVP in ASP.NET. He is the author of ASP.NETData 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 yearsin Microsoft .NET and its related technologies. He has authored articles forsome 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 thesearticles have been selected at http://www.asp.net(Microsoft s Official Site on ASP.NET). Joydip also was a community creditwinner at http://www.community-credit.coma number of times. He is currently working as a Senior Consultant in areputable company in Hyderabad, India. He has years of experience in designingand architecting solutions for various domains. His technical strengths includeC, C++, VC++, Java, C#, Microsoft .NET, AJAX, Design Patterns, SQL Server,Operating Systems, and Computer Architecture. Joydip blogs at http://aspadvice.com/blogs/joydipand spends most of his time reading books, blogging, and writing books andarticles. His hobbies include watching cricket and soccer, and playing chess.Contact Joydip via e-mail at mailto:[email protected]and view his MVP profile at https://mvp.support.microsoft.com/default.aspx/profile/joydip.        

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like