I get the exception "The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements." when I use TransactionScope to wrap large number of DB updates that occur in multiple calls to UpdateItem sub routine. Can someone help understand what this error means and the right way to restructure code while retaining retaining the ability to rollback if anything within the foreach fails (throws an exception)
static void Foo()
{
using (TransactionScope transactionScope = new TransactionScope())
{
foreach (var item in items)
{
UpdateItem(item);
}
transactionScope.Complete();
}
}
static void UpdateItem(string item)
{
using (MyDataContext db = new MyDataContext)
{
:
:
db.Table1.InsertOnSubmit();
:
db.SubmitChanges();
:
:
db.ExecuteCommand(); // I get the exception here
:
db.Table2.InsertOnSubmit();
:
db.SubmitChanges();
:
}
}