Frage

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.

War es hilfreich?

Lösung

you need to give command object to ExecuteNonQuery

do something like this:

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

    cmd.ExecuteNonQuery(); 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top