.NET Exception Not Detected When Using RAISERROR Function in SQL Server

I read so much about the RASIERROR function and its requirements for creating an exception in .NET, but I still cannot get the exception. Here is an SP snippet:

BEGIN
    RAISERROR('This is a custom error message.', 16, 1);
    RETURN;
END

And here is the .NET code snippet:

        cmd.Connection = conn
        cmd.ExecuteNonQuery()

        'Close the db connection, and local dispose
        conn.Close()

    Catch ex As SQLException
        Dim msg As String = ex.Message
    End Try

So, I think I covered the basics:

  • Severity level above 10 (using 16)
  • Catching the type of SQLException (although it inherits from Exception, so it doesn't matter, I would have thought
  • Using 'ExecuteNonQuery'
  • In SQL Enterprise Manager, when I start the stored process directly, I get the error message : "Msg 50000, Level 16, State 1, Line 16 This is a custom error message.

So what am I missing? Any help is appreciated, thanks!

+3
2

:

SqlConnection conn = new SqlConnection("...");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "errTest";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    conn.Open();
    cmd.ExecuteNonQuery();

CREATE PROCEDURE errTest
AS
    RAISERROR('This is a custom error message.', 16, 1);
RETURN;
+1

; , , 100%. . "16", sp ( "debug", EM, sp, "debug" ) - ​​ "10", , ,.NET immdeatly . , .

0

Source: https://habr.com/ru/post/1789589/


All Articles