Scott on Writing

Musings on technical writing...

Customizing the TableAdapter

In my Working with Data in ASP.NET 2.0 tutorial series, Tutorial #1 begins the series by creating a Data Access Layer (DAL) using a Typed DataSet. A Typed DataSet is composed of a collection of strongly-typed DataTables and TableAdapters. The DataTables serve as business objects for holding the underlying database data, whereas the TableAdapters function as the DAL, synchronizing the contents of the business objects with the underlying data store.

The code for the TableAdapters is automatically generated by Visual Studio based on their properties and how you configured it in the Create TableAdapter Wizard. However, TableAdapters are implemented as partial classes, meaning it's possible to extend the functionality of the auto-generated TableAdapter by adding your own class file. The benefit of the partial class approach is that you can add methods and properties to the auto-generated TableAdapter without fear of having your modifications eaten the next time Visual Studio wants to regenerate the code. Let's look at how to utilize the fact that the TableAdapter is a partial class.

The TableAdapter's data store-specific details - the connection information, the command-level settings, and so on - are buried in its code as internal and private properties and methods. This makes sense, as the point of the DAL (and OOP in general) is to abstract away implementation details. However, there may be times where you need to update the connection string from the Business Logic Layer (BLL), or change the CommandTimeout for particularly long-running queries. By simply creating a new class file in your project, you can add public properties or methods to the TableAdapter than can access and modify those private and internal properties. For example, the following code adds a public ConnectionString property and a public SetCommandTimeout(timeout) method to the ProductsTableAdapter class (one of the TableAdapters created in the Working with Data in ASP.NET 2.0 tutorial series):

    1 namespace NorthwindTableAdapters

    2 {

    3     public partial class ProductsTableAdapter

    4     {

    5         public string ConnectionString

    6         {

    7             get

    8             {

    9                 return this.Connection.ConnectionString;

   10             }

   11             set

   12             {

   13                 Connection.ConnectionString = value;

   14             }

   15         }

   16 

   17         public void SetCommandTimeout(int timeout)

   18         {

   19             foreach (IDbCommand command in CommandCollection)

   20                 command.CommandTimeout = timeout;

   21         }

   22     }

   23 }

This property and method can then be used in the BLL after creating an instance of the TableAdapter and before issuing the call to access data (such as GetProducts()).

One word of warning - exposing implementation details in the DAL may be necessary for certain scenarios, but don't make a habit of it. Ideally, the DAL and BLL and presentation tier should each be self-contained and not need to “leak” implementation details or settings to other tiers...

posted on Tuesday, June 27, 2006 11:55 AM

Feedback

# Displaying Formatted Source Code in a Web Page 6/30/2006 9:51 AM Scott on Writing

# re: Customizing the TableAdapter 6/30/2006 11:41 AM brad

hmmmmmmm

Looks bland in the rss feed :-)

# re: Customizing the TableAdapter 7/4/2006 10:39 PM KK

Can this be used to create a new connection string based upon each user's own selection of database? - Or does it set the connection for all users?

# re: Customizing the TableAdapter 7/5/2006 7:30 AM Scott Mitchell

KK, it sets the connection string for that particular instance of the TableAdapter. So to change the connection string from the 'default' one it would need to be set each time you wanted to work with data via the TableAdapter.

# re: Customizing the TableAdapter 7/7/2006 9:19 AM Matt

Hi Scott. Love the tutorials. The best I've found. My question is how to Insert, Edit and Delete. I went through all 15 tutorials.

When I enable editing in a GridView, I get an error message. Do you have another tutorial in the works? Help!!! thanks again.

# re: Customizing the TableAdapter 7/7/2006 9:27 AM Scott Mitchell

Hi Matt, thanks for your kind words.

In total, there will be _46_ tutorials in the current series (and probably another series after that!). Anywho, the next batch of tutorials, slated to be published this coming Monday, July 10th, will cover inserting, updating, and deleting data with the ObjectDataSource, GridView, FormView, and DetailsView controls.

# re: Customizing the TableAdapter 7/7/2006 11:03 AM Matt

Scott, thanks, I look forward to the next batch. I guess I have the weekend off. World Cup! Go France! I'll be sure to check on Monday. Will they be published on ASP.NET, as before? Cheers.

# re: Customizing the TableAdapter 7/7/2006 11:05 AM Scott Mitchell

Yes, the tutorials will continue to be published on www.asp.net, specifically http://www.asp.net/Learn/DataAccess/

Thanks

# re: Customizing the TableAdapter 7/10/2006 3:13 PM Undying

Hi Scott!

I love this Tutorials. Defintly! Just one problem comes up to me:

You use on the Update or Insert in the BLL as List with all the diffrent variables.

In my case i use always a Custom Class/Entity to bind it to the Grid.

The Update in the BusinessLogic looks like
Update(obj as SomeBusinessobject)

Works fine. If I add a field it isn't a Problem for me and I dont have to add it on more palces.

On the other hand I get into troubles if I have ReadOnly Fields in my CustomObject. (like a Total Value or potential the ID (my PK))
Is there any way to tell the CustomObjectClass/Datasource/Gridview or whatever that it shouldn't try to write Readonly Values in the Businessobject?

Thank you for your great work!

Best regards from Austria!

# Displaying Formatted Source Code in a Web Page 7/11/2006 6:21 PM Community Blogs

In my last blog entry you may have noticed that my code snippet looks rather spiffy. Here, let me show

# re: Customizing the TableAdapter 1/1/2010 12:44 AM Sunil.R

Hi Scott,
I tried the code you had given above, but Still am getting Timeout error. please help me.

Here is my BLL class :

Namespace AssignLrToGDNPagingTableAdapters

Partial Public Class LrTransferTableAdapter
Public Property ConnectionString() As String
Get
Return Me.Connection.ConnectionString
End Get
Set(ByVal Value As String)
Connection.ConnectionString = Value
End Set
End Property
Public Sub SetCommandTimeout()
Dim command As IDbCommand
For Each command In CommandCollection
command.CommandTimeout = 360
Next
End Sub
End Class
End Namespace


the CommandTimeOut is not at all recognising i think,

one small change i have done in the above code is, hardcodded the time to 6min (360).

i also tried to give timeout expression in the Xml file but not recognising.

Please help me, am trying this from 3 days. :-(

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles