Click here to Skip to main content
14,302,993 members

LinqDataSource with ASP.NET data controls

Rate this:
2.69 (8 votes)
Please Sign up or sign in to vote.
2.69 (8 votes)
18 Jun 2008CPOL
Using LinqDataSource with ASP.NET data controls like gridview and dropdown list
Note: Please vote and give your feedback on this series so that future efforts can be improved / aligned with your expectation. Thanks!

Topics covered
Intro, LINQ to SQL class file, LINQ DataSource and .dbml file structure DataContext class, writing LINQ in C# 3.0 and parameter zing LINQ queries change underlying TSQL on the fly using LinqDataSource and part of CRUD operation [update and delete] Insert using LinqDataSource.Insert() and managing database connection via web.config
LinqDataSource Part I LinqDataSource Part II LinqDataSource Part III LinqDataSource Part IV

Using LinqDataSource with ASP.NET data controls

On one of my projects I used LINQ. Since this was my first LINQ project, I decided to use Linqdatasource to fetch data from database. Another reason for using LinqDatasource is because of its in-built support for grid view paging and sorting functionality.

One can complete this task in 15/ 20 minutes. Important point covered in this part includes LINQ to SQL class file, LINQ DataSource and .dbml file structure Part II of this article is available here. Part III is available here

If you use Linqdatasource with Gridview and enable the paging, LINQ will write a decent paging query. I remember writing my own paging logic in stored procedure and was impressed with T-SQL generated by LINQ. LINQ is doing the same thing I did inside my stored procedure. [How LINQ queries work is byond the scope of this article. But one good explanation can be found on Mike Taulty’s blog]

So this article is a gist of LinqDataSource control. We will first create a web application project. Our aim is to fetch the data from customer table and display it using a gridview control. In part II, we will add a dropdown box to filter this gridview.

If you are new to LINQ, please visit ScottGu’s blogpost]. Note: you will need Visual Studio 2008 to work with this sample project.

Now on to the LinqDatasource… Create a new web application project in VS 2008.


Please enable Server Explorer from view menu if it’s not visible. From Server explorer, right click on Data Connection and click on Add Connection. Follow the wizard to add a database connection. Once you are done with database setup, your Server explorer will display the database objects. This view is just like an object browser in SQL Server.


We will be using a customer table in this project. But before we start working with database, we will need LINQ to SQL classes. Right click on your project in solution explorer and from add new item selection, choose LINQ to SQL classes file as shown below


Once you are done with adding LINQ to SQL file to your project, you can start working with the database. Note that Customer.dbml file is added to our project as shown below


Open the customer.dbml file and drag-n-drop the customer table from server explorer on the surface of Customer.dbml file as shown below


That’s it! We are done with setting up a database connection – now we can work with customer table in our project. Let’s browse customer table in VSS


Save the project and try building it. It should build without errors or warnings. After you are done with building your project add a grid view and LinqDatasource to the default.aspx page as shown below


Configure data source as shown below. Please don’t change anything on configure data source page. Defaults are just fine and will work like a charm – just hit the next button and select customer table from the dropdown list as shown in the following sequence




Click the finish button to complete this task. We will get back to where option once we complete the step 1.

Now set this datasource as a source for our grid view. Build and run the project.


That’s it – we are done with fetching data from database and displaying it on the webpage using LINQ.


Please open SQL Server query profiler and watch the TSQL generated by LINQ. It is not much different than the one you will write to accomplish such task. In part II we will filter the result set based on the dropdown list selection. We will use customer_type column for this filter operation. For example, display only corporate customers. We will also enable paging on grid view and analyze underlying TSQL.

Part III of this article is available here

Best luck and happy programming!

Note: Please vote and give your feedback on this series so that future efforts can be improved / aligned with your expectation. Thanks!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Wells Fargo
United States United States
MS Computer Science + Information Science
Architect at Wells Fargo [Wells Fargo Dealer Services -WFDS]
Blog: Profile

abhijit gadkari
abhijit dot gadkari at gmail dot com

Comments and Discussions

GeneralI have a Exception Pin
jason_mf5-Mar-10 2:37
memberjason_mf5-Mar-10 2:37 
GeneralGood for beginers Pin
StonePit9-Jun-08 20:40
memberStonePit9-Jun-08 20:40 
AnswerRe: Good for beginers Pin
abhigad9-Jun-08 21:28
memberabhigad9-Jun-08 21:28 
GeneralRe: Good for beginers Pin
StonePit10-Jun-08 4:15
memberStonePit10-Jun-08 4:15 
AnswerRe: Good for beginers Pin
abhigad10-Jun-08 6:20
memberabhigad10-Jun-08 6:20 
GeneralWorking with LINQ to SQL Classes and LinqDataSource Controls Pin
Sheo Narayan8-Jun-08 15:48
memberSheo Narayan8-Jun-08 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 8 Jun 2008


16 bookmarked