Stored procedure :
Procedure GetLocalConfig
(
@ConfigId int
)
AS
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION
SELECT 1/0
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
RETURN
END CATCH
.Net code calling the Stored Procedure :
try
{
return DatabaseFactory.CreateDatabase(ConnectionStringConfigName)
.ExecuteSprocAccessor(DbCommands.GetLocalConfig, new LocalConfigDtoMapper(), configId)
.Single();
}
catch (Exception ex)
{
// Exception : Sequence contains no elements???
}
If I execute this SP from SSMS I get the actual exception :
ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage
8134 16 1 GetLocalConfig 54 Divide by zero error encountered.
I would like to see this exception in my try catch block for .Net however I keep getting Sequence contains no elements.
After some research I found I could RAISEERROR inside my stored procedure Try catch block :
DECLARE @errnum nchar(5), @errmsg nvarchar(2048);
SELECT
@errnum = RIGHT('00000' + ERROR_NUMBER(), 5),
@errmsg = @errnum + ' ' + ERROR_MESSAGE();
RAISERROR (@errmsg, 16, 1);
Now when I run the SP using SSMS I get :
Msg 50000, Level 16, State 1, Procedure GetLocalConfig, Line 70
8134 Divide by zero error encountered.
But still .Net is giving me the same exception, how do I get the exception to be passed over to .Net in this case?