getting pointer to proper dataAdapter from dataset or dataTable

A

astro

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
dataAdapters.

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("", "",
DataViewRowState.ModifiedCurrent))
myDA = <<<<< figure out correct dataAdapter
here>>>>>> <<<=====NEED SUGGESTION HERE
myDA.Update(ds)
End If

bSaveMe = False

Next
Exit Sub
End Sub
======================================
 
C

Cor Ligthert [MVP]

Astro,

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
right?

Cor
 
A

astro

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
etc.....
======================

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
dataAdapter.....

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......
 
C

Cor Ligthert [MVP]

Astro,
.....but I wonder if this information is not stored in the DataSet
itself......
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.
http://www.windowsformsdatagridhelp.com/default.aspx?ID=6510b464-76bf-43a9-9707-5595f8147624


http://msdn.microsoft.com/library/d...emdatasqlclientsqldataadaptermemberstopic.asp


http://msdn.microsoft.com/library/d...mdatasqlclientsqlcommandbuilderclasstopic.asp


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,

Cor


Cor
 
A

astro

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 vb.net 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),
System.Data.SqlClient.SqlDataAdapter)
If da.TableMappings(0).DataSetTable.ToString.ToLower =
ads.Tables(0).TableName.ToString.ToLower Then
'have match - this da is the source for the passed
dataset
Try
da.Update(ads)
Me.DS1.AcceptChanges()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End If

End If
Next
=====================================
 

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

Top