0

RAISERROR でストアド プロシージャを使用しています。SP によって発生したエラーは、c#.Net の try catch ステートメントではキャッチされません。SQL 接続に SqlDataSource を使用し、データを更新するために SqlDataSource_Updating イベントを使用しています。

SQL コード:

DECLARE @count int
@count=0
   if(@count = 0)
   begin
   RAISERROR('Updation failed. record already exists', 16, 1) 
  end

c# コード:

protected void sqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
  try
  {
    // Assigns the update command parameter
    e.Command.Parameters.Add(new SqlParameter("@fag", Flg));
    e.Command.Parameters.Add(new SqlParameter("@Date", DateTime.Now));

  }
  //catch (SqlException ex)
  //{


  //}
  catch (Exception ex)
  {
    // If so, checks the Exception message
    if (ex.Message == "Updation failed. record Not exists")
    {
        // Display the details of the exception to the user
        lblMessage.Text = " record already exists";
    }

    // Writes the error in log file
    WriteErrorLog(ex.Message , "sqlDataSource_Updating");
  }
}

SQL データソース:

                SelectCommand="SELECT [ID], [Name], [Flg] FROM [Master] ORDER BY Name "  

                InsertCommand="exec [Master_Insert] @ID, @Name, @Flg, @createdDate, @createdBy, @updatedDate, @updatedBy"

                UpdateCommand="exec [Master_Update] @ID, @Name, @Flg, @updatedDate, @updatedBy"

                 DeleteCommand="DELETE FROM Master WHERE ID = @ID" 
                            oninserted="sqlDataSource_Inserted" 
                            oninserting="sqlDataSource_Inserting" 
                            onupdated="sqlDataSource_Updated" 
                            onupdating="sqlDataSource_Updating"> 
                </asp:sqldatasource>

よろしく ジーサ

4

3 に答える 3

1

Updatingイベントは、実行中の SqlCommand をカスタマイズするために使用されますが、コマンドは実行されません。try/catch ブロックは、カスタマイズ コールバックではなく、コマンドが実行される実際の場所をラップする必要があります。

于 2009-11-11T19:07:24.323 に答える
0

返信ありがとうございます。

sqlDataSource_Updated で e.Exception を使用することで、私の問題は解決しました。

protected void sqlDataSource_Updated(object sender, SqlDataSourceStatusEventArgs e)

{

       if (e.Exception != null)
            {

                // If so, checks the Exception message

                if (e.Exception.Message == "record already exists")

                {

                }
         }
}
于 2009-11-12T11:41:13.383 に答える
0

私がすぐに気づいたことの 1 つは、ストアド プロシージャが "更新に失敗しました。レコードは既に存在します" というエラー メッセージが、try/catch でチェックインしているテキスト文字列 "更新に失敗しました。レコードが存在しません" と一致しないことです。ブロック。

于 2009-11-11T12:20:33.123 に答える