user9756498
user9756498

Reputation:

How to trigger SQL Server database/table error messages from VB.net

I want to trigger SQL Server error messages while executing a stored procedure.

Example

Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_TableX_$RateCard_Box". The conflict occurred in database "TestDB", table "dbo.TableX".

I want to know if this error trigger in procedure, how can I popup meaningful message to user.

I execute the SQL Server stored procedure from VB.net.

Upvotes: 1

Views: 186

Answers (1)

StepUp
StepUp

Reputation: 38199

It is possible to use TRY CATCH statements and throw Message, Severity and State in CATCH statement:

BEGIN TRY
    -- Your code here...            
END TRY   

BEGIN CATCH             
    DECLARE @Message varchar(MAX) = ERROR_MESSAGE(),
        @Severity int = ERROR_SEVERITY(),
        @State smallint = ERROR_STATE()

    RAISERROR(@Message, @Severity, @State)
END CATCH

Upvotes: 0

Related Questions