getting pointer to proper dataAdapter from dataset or dataTable



I would like to build some generic code that is able to figure out the
correct dataAdapter to apply changes to given a form with several

Any suggestions on the following?

Thank you.

Private Sub SaveChanges(ByVal aContainer As Object)
Dim dt As DataTable
Dim ds As New DataSet
Dim bSaveMe As Boolean
Dim myDA As System.Data.SqlClient.SqlDataAdapter

For Each dt In Me.DS1.Tables
If Not dt.GetChanges(DataRowState.Modified) Is Nothing Then
bSaveMe = True
End If

If bSaveMe Then
ds.Merge(dt.Select("", "",
myDA = <<<<< figure out correct dataAdapter
here>>>>>> <<<=====NEED SUGGESTION HERE
End If

bSaveMe = False

Exit Sub
End Sub

Cor Ligthert [MVP]


Probably you want to use the dataadapter created using the designer in your
generic code.

It is an easier approach to create the dataadapter than completly by code.

However before I write to much maybe can you tell if my expectation is



Thanks Cor - I end up reading your posts a lot and have found them helpful

I want to have a 'system' where I do not have to write a line of code for
each dataAdapter on a form. On the form I currently am working on I have 7
SQLDataAdaptors --

The following allows me to catch changes on any datagrid on a form without
'hardCoding' the names of each table/datagrid....
For Each dt In Me.DS1.Tables
If Not dt.GetChanges(DataRowState.Modified) Is Nothing

If the .NET object library allows me to find each table's DataAdapter (in my
Dataset) I can write a loop that walks through every table and apply changes
back to the database....again without hardcoding the names of each

Is this clear?

If I must I can use the table name from the Dataset to loop through all the
dataAdapters on my form till I get a string match......but I wonder if this
information is not stored in the DataSet itself......

Cor Ligthert [MVP]

.....but I wonder if this information is not stored in the DataSet
It will be in the next version of VBNet.

However see by instance this sample how to make your dataadapter by code,
than you don't have it on your screen.

However you need the updatecommand, insertcommand, deletecommand properties
from the dataadapter. For that are two approaches, you use first the
designer and copies them from the designer part, or you use the
commandbuilder (which goes fine by simple selects).

See some threads I wrote about above.

It has not really much sense if you use one dataadapter.

I don't like all those dataadapters in my form as well. I find it too a nice
approach to create a component (selecting an item component) and use that to
make a databaseclass (databaselayer).

I hope this helps,




Thanks Cor ))

this is what i ended up doing (with some code snippets lifted from

5. enders Oct 11 2002, 9:30 am post on this newsgroup:

Private Sub SaveChanges_DA(ByVal ads As DataSet)
'saves are at table-level scope
'need to find correct dataAdapter for passed dataset
'this info is not available in this version of dataset object
'assumptions: 1) ds has one table and da has one table

Dim propInfos() As System.Reflection.PropertyInfo
Dim propInfo As System.Reflection.PropertyInfo

propInfos = Me.GetType.GetProperties()

For Each propInfo In propInfos
If propInfo.PropertyType.FullName =
"System.Data.SqlClient.SqlDataAdapter" Then
Dim da As System.Data.SqlClient.SqlDataAdapter
da = CType(propInfo.GetValue(Me, Nothing),
If da.TableMappings(0).DataSetTable.ToString.ToLower =
ads.Tables(0).TableName.ToString.ToLower Then
'have match - this da is the source for the passed
Catch exc As Exception
End Try
End If

End If

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question