C# 无法捕获gridview异常
我试图在更新gridview时捕获异常。 代码是 异常处理的代码是C# 无法捕获gridview异常,c#,asp.net,database,gridview,C#,Asp.net,Database,Gridview,我试图在更新gridview时捕获异常。 代码是 异常处理的代码是 protected void ProductGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e) { if (e.Exception != null) { Master.ErrorMessage = "Cannot Update Record"; e.ExceptionHandled = true; }
protected void ProductGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
Master.ErrorMessage = "Cannot Update Record";
e.ExceptionHandled = true;
}
else
{
Master.ResultMessage = "Record Updated Succesfully";
}
但我仍然得到了错误:UPDATE语句与外键约束FK_Products_Suppliers冲突。冲突发生在NORTHWIND数据库表dbo.Suppliers的“SupplierID”列中。
声明已终止。
它曾经工作过一次,但不是每次都工作。
我还收到了viewstate MAC的Asp.net验证失败错误。在UpdateProduct中调用的存储过程中发生异常,因此从未到达/调用事件RowUpdated,因此您无法在那里捕获该异常。实际上它工作过一次,但再次开始显示此问题。并且已检查存储过程。此问题是否可能与viewstate MAC验证失败错误有关。外键约束错误不是ASP.NET错误。ASP仅返回调用ExecuteOnQuery所提供的异常。要捕获行更新中的错误,您必须将ExecuteOnQuery调用包装在try/catch中,以捕获异常并允许函数调用完成,以便RowUpdate事件触发我在捕获异常时应该执行的操作,因为我正在类库文件中编写代码,所以我无法访问用于显示消息的标签。您无需执行任何操作,也可以记录错误,这取决于您的需要。try/catch的主要原因是捕获异常,这样它就不会停止应用程序,并允许函数完成,从而触发其余事件,特别是行更新。
protected void ProductGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
Master.ErrorMessage = "Cannot Update Record";
e.ExceptionHandled = true;
}
else
{
Master.ResultMessage = "Record Updated Succesfully";
}