views:

502

answers:

1

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.

+2  A: 

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();
Robert Harvey
thanks, that was easy.
cdonner
Thanks. This helped solve a problem I faced. However, I am not sure WHY this issues occurs.
Phil
@Phil: The GridView is being bound after the transaction is committed, but while it is still within the scope of the transaction. The error message is saying, "You put this within the transaction, but I can't execute it because you already committed the transaction." Since the transaction is not needed to bind the GridView, it makes sense to just isolate it from the transaction scope.
Robert Harvey