HasChanges()/GetChanges() on a DataSet

If you use the .HasChanges() method of the DataSet, you need to be careful of one thing. I had placed the .Update() command for one of my tables before the call to .HasChanges(). So, the moral of the story is that you seem to need to check for changes before you perform the Update. On the surface, it would seem that this is different from the documentation regarding .HasChanges() in that the DataSet itself is not changed until the .AcceptChanges() method is called. On more examination, my situation was different. I needed to get some additional information from the system so I was querying the data after an insert/update to make sure that I had any calculated changes that those changes showed back up in my DataSet. This seems to override the change checking in HasChanges()/GetChanges(). Anyway, moving the insert/update till after the check for changes has resolved the problem.

One word of warning with HasChanges()/GetChanges().  I was talking to Sahil Malik and he said that HasChanges()/GetChanges() is prone to failure in .NET 1.x due to some problems with large Datasets/Datatables.  He said that HasChanges()/GetChanges() has been re-written for .NET V2, so, if this type of code fails on you in .NET 1.x, its not my fault.  :-)

'Save Update information to the History Table.
If dsActivity.HasChanges(DataRowState.Modified) Then
   For Each dtData In dsActivity.Tables
      dtDataChange = dtData.GetChanges(DataRowState.Modified)
      If Not (dtDataChange Is Nothing) Then
         For j = 0 To (dtDataChange.Rows.Count - 1)
            For i = 0 To dtDataChange.Columns.Count - 1
               If Convert.ToString(dtDataChange.Rows(j)(i, DataRowVersion.Original)) <> Convert.ToString(dtDataChange.Rows(j)(i, DataRowVersion.Current)) Then
                 SaveHistory(strActivityId, strUserId, dtDataChange.TableName, dtDataChange.Columns(i).ColumnName, RecordActionArray(RecordAction.Updated), _
                    Convert.ToString(dtDataChange.Rows(j)(i, DataRowVersion.Current)), Convert.ToString(dtDataChange.Rows(j)(i, DataRowVersion.Original)), sqlCn, sqlTx)
                End If 
             Next 
          Next 
       End If
       If Not (dtDataChange Is Nothing) Then
           dtDataChange.Dispose() 
           dtDataChange = Nothing
       End If 
   Next
End If

2 Comments

  • Hey Wally,



    Do you believe in sublimnal voices?



    &lt;sublimnalvoice&gt;

    .. Blog post about this coming from Sahil in the near future ..

    &lt;/sublimnalvoice&gt;



    Wow, I had no idea sublimnalvoices could predict future.



    - SM

  • I've lost count of the # of times my misplacing AcceptChanges() has caused. :)



    You'd have thought I would have learned by now.

Comments have been disabled for this content.