Home > DeveloperSection > Forums > ADO.Net Transaction when Command's transaction is not set
Tanuj Kumar

Total Post:134

Posted on    June-19-2013 4:06 AM

 ADO.Net ADO.Net 

 1 Reply(s)
 1400  View(s)
Rate this:
Hi Expert, 

I came across a sample on ADO.Net, where a transaction was being done without setting the command's transaction property as in code below.

Is this possible Or one needs to explicitly set the command's transaction property?

// Start a local transaction.
SqlTransaction sqlTran = connection.BeginTransaction();

// Enlist a command in the current transaction.
 SqlCommand command = connection.CreateCommand();

Any help on above is really appreciated.

Sumit Kesarwani

Total Post:378

Posted on    June-19-2013 8:45 AM

Hi Tanuj,

That should throw a runtime exception. That is, if you have an active transaction on a SqlConnection and do not assign a reference to the corresponding SqlTransaction to the SqlCommand.Transaction property and attempt to execute the command, you should get an exception.

In short, set the Transaction property explicitly when executing a command on a connection with an active transaction.

Don't want to miss updates? Please click the below button!

Follow MindStick