0

“/”应用程序中的服务器错误。无法在具有唯一索引“UK_Articles”的对象“dbo.Articles”中插入重复的键行。该语句已终止。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:无法在具有唯一索引“UK_Articles”的对象“dbo.Articles”中插入重复的键行。该语句已终止。

我可以代替内部服务器错误 500 显示英国错误吗?我可以捕捉到错误并以某种方式显示它吗?

编辑:

 try
        {
            NHibernateSession.Save(entity);
        }

        catch (SqlException sex )
        {
            if (sex.Message.Contains("with unique index"))
                throw new UniqueConstraintException("UK ERROR");

            throw;
        }

但是 NHibernateSession.Save(entity) 总是抛出 GenericADOException 而不是 SqlException。我想在 nhibernate 中捕获插入,以便我可以在全球范围内对其进行操作。

4

2 回答 2

2

我假设您正在使用 Sql Server。

您需要一个try catch块来处理SqlException

try
{
    // run inser or update query here
    myObj.InserRow();
}
catch (System.Data.SqlClient.SqlException ex)
{
    if (ex.Number == 2601)
    {
        //"duplicate entry found!";
    }
    else
    {
        // some other kind of sql related exception
    }
}
catch (Exception ex)
{
    // any other exception
}
于 2011-03-15T09:34:29.110 回答
1

您可以使用 web.config 显示手动错误

<customErrors mode="On" defaultRedirect="~/ErrorPage.aspx">
  <error statusCode="500" redirect="~/500Error.aspx" />
</customErrors>
于 2011-03-15T09:23:27.680 回答