Saturday, November 17, 2012 4:57 AM
I m handling master/detail records scenario by following method:
I want to ask a question that how can i apply Begin Trans, Commit Trans & RollBack Trans for the above Scenario that if master is saved then the detail should be saved. Otherwise it should rollback.
Thanks in adv.
Saturday, November 17, 2012 6:00 AM
please try this model
Dim SqlTran As SqlTransaction = Nothing Dim SqlCmd As New SqlCommand SqlCmd.Connection = Gc_Con 'Connection object Try SqlTran = Gc_Con.BeginTransaction(IsolationLevel.ReadCommitted) SqlCmd.Transaction = SqlTran SqlCmd.CommandTimeout = 300 Catch ex As Exception MsgBox("Data Access Error" + ex.message, MsgBoxStyle.Critical, Me.Text) End Try Try AddParamSalesMaster(SqlCmd) ' a function to add parameters for master with a out parameter @returnvalue SqlCmd.CommandType = CommandType.StoredProcedure SqlCmd.CommandText = "sp_salesmaster_ins" SqlCmd.ExecuteNonQuery() Dim Ll_SalesId As Long Ll_SalesId = SqlCmd.Parameters("@returnvalue").Value If Ll_SalesId < 0 Then ' do as per your situation Throw New Exception("Insertion Failed") End If ' now do the detail insertion, if any system error happens , in catch all previous transactions will roll backed. ' or if any business kind of error happens you can throw an error to rollback all 'if everything perfect then commit SqlTran.Commit() Catch ex As Exception SqlTran.Rollback() Cmb_Customer.Focus() Exit Sub Finally SqlTran.Dispose() End Try
- Edited by Johnson T A Saturday, November 17, 2012 6:01 AM
Saturday, November 17, 2012 6:11 PM
Check the below link for
- Proposed As Answer by Kalman TothMicrosoft Community Contributor, Moderator Sunday, November 18, 2012 12:14 AM
- Marked As Answer by Muhammad Kashan Khan Monday, November 19, 2012 4:48 AM
Saturday, November 17, 2012 7:04 PM
You can also make stored Procedure to do your required task. Better to do in sp level so that you can get more better performance.
Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/
- Proposed As Answer by Kalman TothMicrosoft Community Contributor, Moderator Sunday, November 18, 2012 12:13 AM