我正在从Gridview发送的事件中进行级联删除。删除在交易中。这是简化的代码:
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();
}
对网格的DataBind()方法的调用失败,出现此异常: “目前的TransactionScope已经完成。” 为什么呢?
当然,TransactionScope在那时完成了,它应该完成。当我删除TransactionScope时,它可以工作。
答案 0 :(得分:14)
将BindGridView()移到事务范围之外。
using (TransactionScope ts = new TransactionScope())
{
try
{
//delete some data
db.SubmitChanges();
ts.Complete();
}
catch (Exception ex)
{
// handle error
}
finally
{
db.Dispose();
}
}
BindGridView();