G
Guest
I have a winform datagrid where a user can modify more than one row at a
time. A button calls theDatapter.Update() to persist changes to db. I wrap
the Update in a client-side transaction and
commit-on-success/rolllback-on-failure. In addition, exception-handler code
invokes RejectChanges to undo user modifications to the DataTable. Now,
here's the problematic scenario: a user modified two rows where the
modification on the 2nd row causes a db error (e.g., constraint violation).
The problem is that RejectChanges won't rollback the DataTable modification
to the first row but only for the 2nd row. At this point, the 1st row's value
and its db row counterpart are no longer in sync. Here's my code:
' N.B.: m_* variables are module-level.
Dim trans As SqlTransaction
Try
m_conn.Open()
trans = m_conn.BeginTransaction
m_da.SelectCommand.Transaction = trans
m_cmdBuilder.RefreshSchema()
m_da.Update(m_ds, "Authors")
trans.Commit()
MessageBox.Show("Update OK!")
Catch exc As Exception
m_ds.RejectChanges()
If Not trans Is Nothing Then
trans.Rollback()
End If
MessageBox.Show(exc.Message)
Finally
m_conn.Close()
End Try
Any workarounds for this? Thanks in advance =)
time. A button calls theDatapter.Update() to persist changes to db. I wrap
the Update in a client-side transaction and
commit-on-success/rolllback-on-failure. In addition, exception-handler code
invokes RejectChanges to undo user modifications to the DataTable. Now,
here's the problematic scenario: a user modified two rows where the
modification on the 2nd row causes a db error (e.g., constraint violation).
The problem is that RejectChanges won't rollback the DataTable modification
to the first row but only for the 2nd row. At this point, the 1st row's value
and its db row counterpart are no longer in sync. Here's my code:
' N.B.: m_* variables are module-level.
Dim trans As SqlTransaction
Try
m_conn.Open()
trans = m_conn.BeginTransaction
m_da.SelectCommand.Transaction = trans
m_cmdBuilder.RefreshSchema()
m_da.Update(m_ds, "Authors")
trans.Commit()
MessageBox.Show("Update OK!")
Catch exc As Exception
m_ds.RejectChanges()
If Not trans Is Nothing Then
trans.Rollback()
End If
MessageBox.Show(exc.Message)
Finally
m_conn.Close()
End Try
Any workarounds for this? Thanks in advance =)