Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find all the table adapters on a form?

Status
Not open for further replies.

Jasen

IS-IT--Management
May 1, 2002
435
US
It seems bizarre to me that this is so difficult.
You can iterate the controls collection and find things, you can iterate me.components.components, but nowhere can I find where a form keeps a collection of its early-bound table adapters. When you add them in at design time and bind them to a control, it shows them on the design window, and it creates them in the design.vb/cs file, and they're available as a property of your form object... but I can't find a way to browse through them and pick one in code, like you can with the control collection (I.E. me.controls.Item("controlname")).

I thought for sure they'd be in components.components, but they're not. The bindingsource objects are in there.
Has anyone ever heard of a way to do this?

If not, maybe my strategy here needs refinement.

Here's the problem:

I'm writing an app that acts as a front end to a pair of databases. The two databases are unrelated, but I have the need to pull data from one to manipulate things in the other. Easy enough, except this pair of databases is duplicated across 7 sites--meaning 14 databases. This app needs to be able to switch which site it's looking at at the click of a button. Usually one would solve this by simply creating one DataTable and one TableAdapter in the dataset for each table/query and just switch connection strings when you want to switch sites. Unfortunately, there are some slight differences between the table structures between these site's databases--meaning the underlying query used to create the table adapter will be different. I need a table adapter for each table at each site.

My solution:
I have two DataSet's in my project, one for each database. Inside each dataset I create a datatable and tableadapter for each table or stored procedure I want to access at each site, so seven of each. They're all named consistently: "SiteTableName" and "SiteTableNameTableAdapter".

In my app I have a global variable to hold the current site name, which gets set when the user selects the site they want to view.

I partial classed the system.windows.forms.form class to extend the functionality of all my forms, and added two hashtable collections to the new subclass--one to hold all the DataTables and one to hold all the TableAdapters. So now I can get a reference to any TableAdapter on my form by doing Me.TableAdapters.GetByName(Site & "TablenameTableAdapter"). Basically, I created a way to access a property of my form without knowing the exact name of that property at design time.

All of this works beautifully--except I have to manually populate the collections in my form_load. This is clunky--and it makes maintaining the forms harder. If I add a new TableAdapter, or new bound control (which automatically creates a tableadapter) I have to remember to manually add it to my collection in form_load. I want to be able to automatically find them all and add them (preferably either in the New() or InitializeComponent() methods) automatically.

Is this all a bad idea?
 
I've never worked with a TableAdapter. I try to stay away from wizard-generated data access objects.

So my suggestion would be to use DataSets, DataTables, DataAdapters and Sql/OleDBCommands.

Just change the CommandText for your Select Commands based on the database chosen. Or even better, write stored procedures in the databases to return the same resultset, regardless of the underlying table structures.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top