简体   繁体   English

客户端(C#)的更新过程未捕获过程(SQL Server 2005)中的Raiserror

[英]Raiserror in procedure (SQL Server 2005) is not caught in update process in client side(c#)

I am using stored procedure with RAISERROR. 我正在将存储过程与RAISERROR一起使用。 The error raised by the SP is not caught by the try catch statement in c#.Net. SP引发的错误未被c#.Net中的try catch语句捕获。 I'm using SqlDataSource for SQL connection and SqlDataSource_Updating event to update the data. 我正在使用SqlDataSource进行SQL连接和SqlDataSource_Updating事件来更新数据。

SQL Code: SQL代码:

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

c# Code: 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");
  }
}

SQLDataSource: SQLDataSource:

                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>

Regards Geetha 问候Geetha

The Updating event is used to customize the SqlCommand being executed, but it does not execute the command. Updating事件用于自定义正在执行的SqlCommand,但不会执行该命令。 The try/catch block must wrap the actual place where the command is executed, not the customization callback. try / catch块必须包装执行命令的实际位置,而不是定制回调。

我立即注意到的一件事是您的存储过程正在引发“更新失败。 记录已存在 ”的错误消息与文本字符串“更新失败。 记录不存在 ”不匹配,您正在检查try / catch块。

Thank You for all your replies. 感谢您的所有答复。

By using a e.Exception in sqlDataSource_Updated my problem got solved. 通过在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")

                {

                }
         }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM