Reputation: 458
after successfully saving a transaction using my code...I edit the transaction and then save...then i come up with this error message:
ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
here is my code:
Try
sqlTrans = sqlCon_.BeginTransaction(IsolationLevel.RepeatableRead)
sSQLAdapter_.UpdateBatchSize = 30
sCommand_ = DirectCast(sSQLAdapter_.SelectCommand, SqlCommand)
sCommand_.Connection = sqlCon_
sCommand_.Transaction = sqlTrans
sSQLAdapter.SelectCommand.Transaction = sqlTrans
sSQLAdapter_.Update(sDataSet.Tables(0))
sqlTrans.Commit()
sqlCon_.Close()
Catch ex As Exception
sqlTrans.Rollback()
Finally
sSQLAdapter.SelectCommand.Connection.Close()
sSQLAdapter.SelectCommand.Connection = Nothing
sSQLAdapter.SelectCommand.Transaction = Nothing
sSQLAdapter.SelectCommand.CommandText = ""
sSQLAdapter.SelectCommand.Dispose()
sqlTrans.Dispose()
sqlCon_.Close()
sqlCon_.Dispose()
End Try
Upvotes: 1
Views: 1122
Reputation: 94625
You need to set Transaction object reference for InsertCommand
, UpdateCommand
and DeleteCommand
object of SqlDataAdapter because you're calling Update
method.
MSDN reference document - Using Transactions with a DataAdapter
Sample :
Using Cn As New SqlConnection(CnStr)
Cn.Open()
Using Trans As SqlTransaction = Cn.BeginTransaction
Dim Adp As New SqlDataAdapter(selectQuery, Cn)
Adp.SelectCommand.Transaction = Trans
Dim cmb As New SqlCommandBuilder(Adp)
Adp.DeleteCommand = cmb.GetDeleteCommand()
Adp.InsertCommand = cmb.GetInsertCommand()
Adp.UpdateCommand = cmb.GetUpdateCommand()
Adp.InsertCommand.Transaction = Trans
Adp.DeleteCommand.Transaction = Trans
Adp.UpdateCommand.Transaction = Trans
Dim dt As New DataTable
Adp.Fill(dt)
/* -------------
Perform insert/delete/update on DataSet or DataTable
-------------- */
Adp.Update(dt)
Trans.Commit()
End Using
Cn.Close()
End Using
Upvotes: 1