0

「/」アプリケーションでサーバー エラーが発生しました。一意のインデックス 'UK_Articles' を持つオブジェクト 'dbo.Articles' に重複するキー行を挿入することはできません。ステートメントは終了されました。

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.Data.SqlClient.SqlException: 一意のインデックス 'UK_Articles' を持つオブジェクト 'dbo.Articles' に重複するキー行を挿入できません。ステートメントは終了されました。

内部サーバー エラー 500 の代わりに UK エラーを表示できますか? エラーをキャッチして表示できますか?

編集:

 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 を使用していると仮定します。

SqlExceptiontry catchを処理するにはブロックが必要です

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 に答える