Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 636
cdonner Avatar asked Sep 17 '09 03:09

cdonner


1 Answers

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();
like image 136
Robert Harvey Avatar answered Nov 04 '22 06:11

Robert Harvey