I have the following code which calls a stored procedure. I want to be able to trap any error that occurs during the running of the stored procedure.
try {
using (var connection = GetConnection()) {
using (SqlCommand cmd = connection.CreateCommand()) {
connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "VerifyInitialization";
cmd.Parameters.Add(new SqlParameter("@userId", user.Id));
cmd.Parameters.Add(new SqlParameter("@domainId", user.DomainId));
cmd.ExecuteNonQueryAsync();
}
}
}
catch (Exception ex) {
throw new LoginException(LoginExceptionType.Other, ex.Message);
}
This is the stored procedure, which basically just calls other stored procedures.
ALTER PROCEDURE [dbo].[VerifyInitialization]
-- Add the parameters for the stored procedure here
@userId int,
@domainId int
AS
BEGIN
Begin Try
SET NOCOUNT ON;
Exec VerifyInitializationOfDefaultLocalizationItems
Exec VerifyInitializationOfLayoutLists @domainId
Exec VerifyInitializationOfLayoutListItems @domainId
Exec VerifyInitializationOfLocalizationItems @domainId
Exec VerifyInitializationOfLookupLists @domainId
Exec VerifyInitializationOfLookupListItems @domainId
End try
Begin Catch
-- Raise an error with the details of the exception
DECLARE
@ErrMsg nvarchar(4000) = Error_message(),
@ErrSeverity int = ERROR_SEVERITY();
RAISERROR(@ErrMsg, @ErrSeverity, 1)
End Catch
End
What do I need to do to catch an error in the Stored Proc that will be returned back to C#? Say for example a field name is renamed which prevents one of the stored procs from running. I don't want it to fail silently.
Greg