The more I learn, the more I realize I know verry little...
My biggest handicap is that I started with VB1 (yes, ONE, MS-DOS and even Basica before that), so it is sometimes hard to lose (bad) habbits.
Not really realizing that the (almost) same code can be used in a 100% detached (web) client, I should have anticipated this way of doing things.
Reading MSDN:
The DataAdapter serves as a bridge between a http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx and a data source for retrieving and saving data. The DataAdapter provides this bridge by mapping http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.fill.aspx, which changes the data in the http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx to match the data in the data source, and http://msdn.microsoft.com/en-us/library/system.data.idataadapter.update.aspx, which changes the data in the data source to match the data in the http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx.
....and ...
When an instance of DataAdapter is created, the read/write properties are set to initial values. For a list of these values, see the DataAdapter constructor.
would make one think that the dataadapter should be kept for later usage (.Update)
With the current way of working, as the initial datadapter can be discarded after filling the dataset, but I have to keep the initial SelectCommand somewhere for when I call the SaveChanges with it.
But, more disturbing is that your version of SaveChanges works fine it trans is False. When True and the 'If trans' part is executed, the commands are Nothing, resulting in an error. (I added the transaction also to the SelectCommand), so no progress. Sorry.
For Each itm As UpdateDS In colDS
Dim cmd As New SqlCommand(itm.SelectCommand, connection, transaction)
Dim da As New SqlDataAdapter(cmd)
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
If trans Then
da.InsertCommand.Transaction = transaction
da.DeleteCommand.Transaction = transaction
da.UpdateCommand.Transaction = transaction
End If
da.Update(itm.DS, itm.TableName)
Next
To make life easyer if only one dataset is in play, I added this (kind of overload):
Public Function SaveChanges(ByRef ds As DataSet, ByVal table As String, ByVal selectCmd As String, Optional ByVal trans As Boolean = False) As Boolean
Dim mylist As New List(Of UpdateDS)
mylist.Add(New UpdateDS(ds, selectCmd, "table"))
Return SaveChanges(mylist, trans)
End Function