【发布时间】:2014-12-28 17:45:42
【问题描述】:
我正在使用 Visual Studio 2013 处理 ASP.NET 动态数据实体项目。我的数据源是基于 SQL Server 数据库的 ADO.NET 实体数据模型。
当我尝试插入一个违反唯一键约束的新表条目时,我得到一个非常不友好的错误页面,其中包含整个堆栈跟踪。 是否可以在插入视图中仅显示一条简短的错误消息? (类似于必填字段为空时的错误消息)
到目前为止,我尝试的是在 Insert.aspx.cs
中扩展以下代码protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) {
if (e.Exception == null || e.ExceptionHandled) {
Response.Redirect(table.ListActionPath);
}
}
对此(检查与异常号 2627 的唯一键冲突)
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) {
if (e.Exception == null || e.ExceptionHandled) {
Response.Redirect(table.ListActionPath);
}
else
{
SqlException innerException = e.Exception.InnerException as SqlException;
if (innerException != null)
{
if (innerException.Number == 2627)
{
// Violation of unique constraint
throw new ValidationException("Unique key violation");
}
}
}
}
虽然 Insert.aspx 使用 asp:DynamicValidator 我得到以下异常: “ValidationException 未被用户代码处理。”
有人知道在这里做什么吗?非常感谢。
【问题讨论】:
标签: asp.net entity-framework dynamic-data