![](/img/trans.png)
[英]Raiserror in procedure (SQL Server 2014) does not catch client side(c#)
[英]Raiserror in procedure (SQL Server 2005) is not caught in update process in client side(c#)
我正在將存儲過程與RAISERROR一起使用。 SP引發的錯誤未被c#.Net中的try catch語句捕獲。 我正在使用SqlDataSource進行SQL連接和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");
}
}
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>
問候Geetha
Updating事件用於自定義正在執行的SqlCommand,但不會執行該命令。 try / catch塊必須包裝執行命令的實際位置,而不是定制回調。
我立即注意到的一件事是您的存儲過程正在引發“更新失敗。 記錄已存在 ”的錯誤消息與文本字符串“更新失敗。 記錄不存在 ”不匹配,您正在檢查try / catch塊。
感謝您的所有答復。
通過在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.