"The operation is not valid for the state of the transaction" error and transaction scope
After doing some research, it seems I cannot have two connections opened to the same database with the TransactionScope block. I needed to modify my code to look like this:
public void MyAddUpdateMethod(){ using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { using(SQLServer Sql = new SQLServer(this.m_connstring)) { //do my first add update statement } //removed the method call from the first sql server using statement bool DoesRecordExist = this.SelectStatementCall(id) }}public bool SelectStatementCall(System.Guid id){ using(SQLServer Sql = new SQLServer(this.m_connstring)) { //create parameters }}
When I encountered this exception, there was an InnerException "Transaction Timeout". Since this was during a debug session, when I halted my code for some time inside the TransactionScope, I chose to ignore this issue.
When this specific exception with a timeout appears in deployed code, I think that the following section in you .config file will help you out:
<system.transactions> <machineSettings maxTimeout="00:05:00" /> </system.transactions>
I also come across same problem, I changed transaction timeout to 15 minutes and it works. I hope this helps.
TransactionOptions options = new TransactionOptions();options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;options.Timeout = new TimeSpan(0, 15, 0);using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,options)){ sp1(); sp2(); ...}