Click here to Skip to main content
Click here to Skip to main content

LinqDataSource with ASP.NET data controls

, 18 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

abhigad
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 Pinmemberjason_mf5-Mar-10 2:37 
GeneralGood for beginers PinmemberStonePit9-Jun-08 20:40 
AnswerRe: Good for beginers Pinmemberabhigad9-Jun-08 21:28 
GeneralRe: Good for beginers PinmemberStonePit10-Jun-08 4:15 
AnswerRe: Good for beginers Pinmemberabhigad10-Jun-08 6:20 
GeneralWorking with LINQ to SQL Classes and LinqDataSource Controls PinmemberSheo Narayan8-Jun-08 15:48 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 19 Jun 2008
Article Copyright 2008 by abhigad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid