Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

LinqDataSource with ASP.NET data controls

0.00/5 (No votes)
18 Jun 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here