1

Possible Duplicate:
ExecuteNonQuery requires the command to have a transaction error in my code

strSQL = "insert into............";

SqlTransaction objSqlTransaction = Master.objSqlDbComm.SqlConnectionObject.BeginTransaction();
try
{
    Master.objSqlDbComm.ExecuteNonQuery(strSQL);
    objSqlTransaction.Commit();
}
catch(Exception)
{
    objSqlTransaction.Rollback();
}
finally
{
    objSqlTransaction.Dispose();
}

When I use above code getting error

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.

Community
  • 1
  • 1
lax
  • 518
  • 2
  • 11
  • 26

1 Answers1

1

you need to give command object to ExecuteNonQuery

do something like this:

SqlCommand cmd = new SqlCommand(your_sqlText, your_sqlcon, your_sqlTrans);  

    cmd.ExecuteNonQuery(); 
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70