G
Guest
I have a struck a problem updating adapters when there are database relationships. If I setup two adapters for the Orders, and OrderDetails tables from Northwind, and then I delete an Order with related order details, I get a constraint violation in the database (because I am updating the Order adapter first). If I swap around the order of the updates, then I get a similar exception when I do a insert, because there is no related order for the order details
I have tried wrapping all commands in a transaction. This did not work. I got the same exception raised from the adapter.update line (when I expected the Adapter.Update to be postponed until the Transaction.Commit)
This seems like quite a simple problem that should be able to be solved by transactions. Here is some sample code. Both adapters were configured using the default configuration against the orders and orderdetails table of the Northwind database. The dataset (dsOrders) simply has the Orders and OrderDetails tables on it with one relation on OrderID
Please dont reply if you dont know for sure
Thanks
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Clic
Dim dsTemp As New dsOrder
Me.sqldaOrder.Fill(dsTemp
Me.sqldaOrderDetail.Fill(dsTemp
Me.DsOrders1.Clear(
Me.DsOrders1.Merge(dsTemp
End Su
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Clic
Dim tran As SqlTransactio
Tr
Me.SqlConnection1.Open(
tran = Me.SqlConnection1.BeginTransaction(
Me.SetupAdapter(Me.sqldaOrder, tran
Me.SetupAdapter(Me.sqldaOrderDetail, tran
Me.sqldaOrder.Update(Me.DsOrders1
Me.sqldaOrderDetail.Update(Me.DsOrders1
tran.Commit(
Catch ex As Exceptio
tran.Rollback(
Finall
Me.SqlConnection1.Close(
End Tr
End Su
Public Sub SetupAdapter(ByVal Adapter As IDbDataAdapter, ByVal Tran As IDbTransaction
Adapter.SelectCommand.Connection = Tran.Connectio
Adapter.InsertCommand.Connection = Tran.Connectio
Adapter.DeleteCommand.Connection = Tran.Connectio
Adapter.UpdateCommand.Connection = Tran.Connectio
Adapter.SelectCommand.Transaction = Tra
Adapter.InsertCommand.Transaction = Tra
Adapter.DeleteCommand.Transaction = Tra
Adapter.UpdateCommand.Transaction = Tra
End Su
I have tried wrapping all commands in a transaction. This did not work. I got the same exception raised from the adapter.update line (when I expected the Adapter.Update to be postponed until the Transaction.Commit)
This seems like quite a simple problem that should be able to be solved by transactions. Here is some sample code. Both adapters were configured using the default configuration against the orders and orderdetails table of the Northwind database. The dataset (dsOrders) simply has the Orders and OrderDetails tables on it with one relation on OrderID
Please dont reply if you dont know for sure
Thanks
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Clic
Dim dsTemp As New dsOrder
Me.sqldaOrder.Fill(dsTemp
Me.sqldaOrderDetail.Fill(dsTemp
Me.DsOrders1.Clear(
Me.DsOrders1.Merge(dsTemp
End Su
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Clic
Dim tran As SqlTransactio
Tr
Me.SqlConnection1.Open(
tran = Me.SqlConnection1.BeginTransaction(
Me.SetupAdapter(Me.sqldaOrder, tran
Me.SetupAdapter(Me.sqldaOrderDetail, tran
Me.sqldaOrder.Update(Me.DsOrders1
Me.sqldaOrderDetail.Update(Me.DsOrders1
tran.Commit(
Catch ex As Exceptio
tran.Rollback(
Finall
Me.SqlConnection1.Close(
End Tr
End Su
Public Sub SetupAdapter(ByVal Adapter As IDbDataAdapter, ByVal Tran As IDbTransaction
Adapter.SelectCommand.Connection = Tran.Connectio
Adapter.InsertCommand.Connection = Tran.Connectio
Adapter.DeleteCommand.Connection = Tran.Connectio
Adapter.UpdateCommand.Connection = Tran.Connectio
Adapter.SelectCommand.Transaction = Tra
Adapter.InsertCommand.Transaction = Tra
Adapter.DeleteCommand.Transaction = Tra
Adapter.UpdateCommand.Transaction = Tra
End Su