DataTable.Merge() method help, not persisting changes  
Author Message
newyuppie





PostPosted: .NET Framework Data Access and Storage, DataTable.Merge() method help, not persisting changes Top

vb2005

hi, im implementing a sort of AutoBackup feature for a bound form in my app. every 5 minutes i save the datatable's changes to a XML file. if my app crashes, next time i load the form i get the recover screen, which loads the XML file back in, merges it with the current working datatable (last saved one from database).

problem is when i update the datatable back to the database. the columns that i merged back are not being persisted to the database. the only way to get them to persist is to go to the TextBox bound to the column, make any change there, and then update the datatable. something is wrong here, let me show a bit of the code:

'''' THE CODE THAT SAVES EVERY 5 MINUTES TO XML STORAGE:
If MySQLDataset.HasChanges() Then
Dim stmWriter As New StreamWriter(New IsolatedStorageFileStream( _
FileName, FileMode.Create, isoStorage))

Dim ChangedDataTable As DataTable = MySQLDataset.solicitantes.GetChanges

If (ChangedDataTable IsNot Nothing) Then
ChangedDataTable.WriteXml(stmWriter, XmlWriteMode.WriteSchema)
End If

stmWriter.Close()
stmWriter = Nothing
End If

'''' THE CODE THAT RETRIEVES THE XML INFO BACK AND MERGES IT WITH CURRENT DATASET
Dim stmReader As New StreamReader(New IsolatedStorageFileStream( _
FileName, FileMode.Open, isoStorage))

Dim RecoveredChangesDataTable As New DataTable

RecoveredChangesDataTable.ReadXml(stmRea
der)
RecoveredChangesDataTable.AcceptChanges()

MySQLDataset.solicitantes.Merge(RecoveredChangesDataTable, False)

up until now i have a nice datatable that merged the RecoveredChanges succesfully, and has a RowState of Modified.

'''HERES THE CODE THAT PERSIST THE DATATABLE TO DATABASE
Dim ChangedDataTable As DataTable = MySQLDataset.solicitantes.GetChanges

If (ChangedDataTable IsNot Nothing) Then
Me.SolicitantesMySqlDataAdapter.Update(ChangedDataTable)
MySQLDataset.solicitantes.Merge(ChangedDataTable)
MySQLDataset.solicitantes.AcceptChanges()
End If

just for the record, it doesn't work either if this code read only: Me.SolicitantesMySqlDataAdapter.Update(MySQLDataSet.solicitantes). Reason i did it like shown is because i was gonna implement some sort of error checking of the changes and pass them back once i solved the errors (as suggested by MSDN)

this code above does not work. changes merged back are not persisted, while any modifications i make manually in the form are.

does somebody know if there are any issues with the Merge method or something like that

thanks


.NET Development20  
 
 
Sarah Parra - MSFT





PostPosted: .NET Framework Data Access and Storage, DataTable.Merge() method help, not persisting changes Top

>> up until now i have a nice datatable that merged the RecoveredChanges succesfully, and has a RowState of Modified.

Are you sure the RowStates are Modified for the merged rows When you call AcceptChanges after reading the XML back into the DataTable, that will mark all rows as Unchanged. The only way they will get changed again is if they are being matched to an existing row with a RowState of something other than Unchanged. Since it sounds like you are merging the backed up data with data you just loaded from the database, I would assume that any existing rows in the DataTable are also Unchanged. In this scenario, the result of Merging Unchanged rows with Unchanged rows is Unchanged rows.

Is GetChanges returning Nothing You mention that the changes are not being persisted, but it's not clear to me if you've determined that's because ChangedDataTable is Nothing, or if ChangedDataTable does contain rows, but the .Update call is not causing them to persist for some reason.

If GetChanges returns nothing, then I would expect the update would also not work if you directly update the DataTable itself instead of the result from GetChanges (you have indicated it doesn't work, so that makes sense to me). GetChanges effectively gives you an isolated view of the rows that are going to be touched during the Update call.

Thanks,
Sarah



 
 
xperre





PostPosted: .NET Framework Data Access and Storage, DataTable.Merge() method help, not persisting changes Top

Hi,

Sorry. I removed my post of yesterday since it contains incorrect information.

Thanks Sarah,

Peter.