Click here to Skip to main content
15,880,469 members
Articles / Web Development / XHTML
Article

LinqDataSource with ASP.NET data controls

Rate me:
Please Sign up or sign in to vote.
2.69/5 (8 votes)
18 Jun 2008CPOL4 min read 73.3K   16   6
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.

image001.gif

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.

image002.gif

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

image003.gif

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

image004.gif

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

image005.gif

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

image006.jpg

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

image007.gif

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

image008.gif

image009.gif

image010.gif

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.

image011.gif

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

image012.gif

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!

License

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


Written By
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
Irvine-92618,CA

Comments and Discussions

 
GeneralI have a Exception Pin
jason_mf5-Mar-10 2:37
jason_mf5-Mar-10 2:37 
GeneralGood for beginers Pin
StonePit9-Jun-08 20:40
StonePit9-Jun-08 20:40 
AnswerRe: Good for beginers Pin
abhigad9-Jun-08 21:28
abhigad9-Jun-08 21:28 
GeneralRe: Good for beginers Pin
StonePit10-Jun-08 4:15
StonePit10-Jun-08 4:15 
AnswerRe: Good for beginers Pin
abhigad10-Jun-08 6:20
abhigad10-Jun-08 6:20 
We will cover Update / Edit / Delete in part III.
GeneralWorking with LINQ to SQL Classes and LinqDataSource Controls Pin
Sheo Narayan8-Jun-08 15:48
Sheo 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.