I have a stored procedure on a hosted SQL 2008 instance. In these stored procedures is logic checking for error. I then return an error code using RAISERROR(#####, ##, ###) if there is a problem. This works well enough, but there is a problem with catching such an error.
In Visual Studio 2010 I can catch the error with the correct Class and State, but never the correct Number. I will get a Message like the following in the exception:
Error #####, severity ##, state ###, was raised, but no message with that error number was found in sys.message. If error is larger than 50000, make sure the user-defined message is added using sp_addmessage.
So clearly the error is being raised correctly from the procedure, but the Number returned is always 18054. What am I doing wrong? I don’t care what the text of the message is, and I don’t have permission to run sp_addmessage anyway. However, I would like Number to be correct so I can properly catch the error in my application and handle it properly.
In case it matters, I’m calling the procedure using EF4.
Your error isn’t being raised correctly. The error you’re actually getting, 18054, is a system error which pretty much says,
If you don’t have access to add records to
[sys].[messages]just specify your error number in the message text (msdn):