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

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

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles