I am doing cascading deletes in an event sent from a Gridview. The deletes are in a Transaction. Here is the simplified code:
protected void btnDeleteUser_Click(object sender, EventArgs e)
{
DataContext db;
db = new DataContext();
using (TransactionScope ts = new TransactionScope())
{
try
{
//delete some data
db.SubmitChanges();
ts.Complete();
}
catch (Exception ex)
{
// handle error
}
finally
{
db.Dispose();
BindGridView();
}
}
}
private void BindGridView()
{
DataContext db;
db = new DataContext();
GridView.DataSource = <my query>
GridView.DataBind(); <========Exception
db.Dispose();
}
The call to the grid's DataBind() method fails with this exception: "The current TransactionScope is already complete." Why?
Of course is the TransactionScope complete at that point, and it should. When I remove the TransactionScope, it works.
Move BindGridView() outside of the transaction scope.
using (TransactionScope ts = new TransactionScope())
{
try
{
//delete some data
db.SubmitChanges();
ts.Complete();
}
catch (Exception ex)
{
// handle error
}
finally
{
db.Dispose();
}
}
BindGridView();