我正在对从 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 时,它会起作用。
最佳答案
将 BindGridView() 移到事务范围之外。
using (TransactionScope ts = new TransactionScope())
{
try
{
//delete some data
db.SubmitChanges();
ts.Complete();
}
catch (Exception ex)
{
// handle error
}
finally
{
db.Dispose();
}
}
BindGridView();
关于c# - 错误绑定(bind) Gridview : "The current TransactionScope is already complete",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1436577/