Click here to Skip to main content
12,690,575 members (33,773 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked

LinqDataSource with ASP.NET data controls [Part IV]

, 23 Jun 2008 CPOL
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

In this article, we will cover insert operation and impact of .dbml on web.config. Frankly, figuring out insert operation took some time; it is bit tricky as compared to SqlDataSource. I hope this explanation will be useful.

In part III, we saw how to work with GridView itemtemplate to enable delete option. Here we will add one more button to the collection of buttons [as shown in figure 13 –part III]. We will add insert code in the click event of the Insert button as shown in figure 1

Figure 1


Add the following code in btnInsert click event as shown below in figure 2

Figure 2


As shown in figure 2 - step 1, this is a btnInsert click event. We defined lstNewCustomer ListDictonary from System.Collections.Specialized namespace. In figure 2 - step 2, we got four placeholders mapped against the column name of the customer table. [Refer to figure 1 –part 3 for the schema of customer table].In figure 2 - step 3, we got the values for these columns from grid view footer row. Step 3 will add a new record via Grid View footer row. [<a href="" target=_blank">This is a good explanation on “Use the Grid View to insert a new record”]. Although <a href="" target=_blank">this post is based on SqlDataSource, conceptually, the process of working with LinqDataSource is identical. Replace the code in Button1_Click event of this post with the one shown in figure 2 above.

Another good resource on how to insert a new record using Grid View can be found here

In figure 2 -step 4, we will add input values to ListDictonary lstNewCustomer. Be careful with the ListDictonary key-value collection where key is the column name and value will be the column value to insert.

lstNewCustomer ListDictonary will be used to insert a record through LinqDataSource as LinqDataSource1.Insert(lstNewCustomer) and then we will rebind the Grid View as GridView1.DataBind(). After the record is inserted and Grid View repopulated, we will clear the input textboxes as shown in figure 2 -step 5. Figure 3 will walk you through the insert record lifecycle

Figure 3


As shown above, figure 3.A represents an empty Grid View footer row. 3. B is the row with values ready for insertion. Click on Insert button to insert this record. This will trigger the btnInsert click event as shown in figure 2. As a result of this event, a record will be inserted in customer table.

3. C is the updated Grid View with record inserted in the underlying table. Also, we have cleared the footer row after successful insertion of a record as a result of code shown in figure 2 – step 5.

With this we covered how to edit, update, delete and insert a record using GridView and LinqDataSource. Now is the time to focus on web.config and figure out the underlying database connection plumbing. Did you think about how LINQ to SQL is really talking to a customer table? Where is the database connection string? If yes, you are heading in right direction. If not, this is the time to do so.

Well the code explained from part I through this article will work on your local box / laptop or any machine on which integrated security option is set to true. Refer to figure 4 for more details

Figure 4:


This connectionstring is a result of drag-n-drop we did in part I. [dragging a customer table on .dbml page from server explorer]. If you open the [filename].designer.cs file under your .dbml file – you will find a code like one shown in figure 6.

Figure 5:


As shown in figure 5, .dbml file is named as Customer.dbml . The code in figure 6 is from Customer.designer.cs file

Figure 6:


Compare figure 4 with 6 and focus on connectionstring property in figure 4 with figure 6.1 [underlined code]. This is how LinqDataSource is talking to the underlying database. Code in figure 6.2 explains how the customer table is mapped against the partial class customer.

This code will fail where integrated security is set to false and needs username and password to connect to the database. This is a gotcha that can be fixed by adding username and password to the connection string instead of using integrated security.

Ton load of ORM code is generated by compiler for free. If you really want to explore the power of LinqDataSource, start digging through this auto generated code. You will find many hidden secretes that no book or article will explain. I am on my way there, and will report back any interesting findings.

Till then have a great, productive programming!


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

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 24 Jun 2008
Article Copyright 2008 by abhigad
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid