Error Binding Gridview: "The current TransactionScope is already complete"

cdonner picture cdonner · Sep 17, 2009 · Viewed 11.9k times · Source

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.

Answer

Robert Harvey picture Robert Harvey · Sep 17, 2009

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();