Home All Groups Group Topic Archive Search About

PRINT messages inapproriately ending up in SqlException's message property

Author
9 Jul 2005 2:49 PM
Paul
I posted this question in the VB.Net newsgroup but got no response. This
question is related to both the DotNet Framework and SQL Server, I'm hoping
maybe someone can help me here.

Occasionally my users will try to perform an action and be presented with an
exception raised directly from SQL Server, in example:

SET @err = 'a user-friendly error condition message'
RAISERROR(@err, 16, 1, 1)
RETURN

The exception type thrown is a System.Data.SqlClient.SqlException, and its
message property holds the exception text. Works well for the most part.
However, a lot of times the SQL error conditions happen after a trigger is
fired, and I tend to place informational PRINT statements in my triggers so
I know what they are doing and when. So to give a simplified example to
reproduce this condition...

PRINT 'some non-user-friendly information...'
PRINT 'more non-user-friendly information...'
SET @err = 'a user-friendly error condition message'
RAISERROR(@err, 16, 1, 1)
RETURN

Now the message property in the corresponding
System.Data.SqlClient.SqlException will contain not only the error text, but
(inexplicably) all the PRINT statements before it, even ones made from other
stored procedures and triggers. I don't want users seeing these things as it
confuses them. I can't just chop everything off after the first line because
some multi-line error messages are legitimate to parse and pass to users. Is
there any way I can keep PRINT messages out of the exception text, or will I
have to do something ugly like prefix all PRINT statements with a character
so I can parse them out?

Paul

Author
9 Jul 2005 4:32 PM
Dan Guzman
> Is there any way I can keep PRINT messages out of the exception text,
> or will I > have to do something ugly like prefix all PRINT statements
> with a character so I can parse them out?

One method is to examine the error number and display only those that are
non-zero:

    Try
        command.ExecuteNonQuery()
    Catch ex As SqlException
        DisplayExceptionMessage(ex)
    End Try

    Private Sub DisplayExceptionMessage(ByVal ex As SqlException)
        Dim swErrorMessages As New StringWriter
        Dim sqlError As SqlError
        For Each sqlError In ex.Errors
            If sqlError.Number > 0 Then
                swErrorMessages.WriteLine(sqlError.Message)
            End If
        Next
        MessageBox.Show(swErrorMessages.ToString())
        swErrorMessages.Close()
    End Sub

--
Hope this helps.

Dan Guzman
SQL Server MVP

Show quote
"Paul" <noone@executespammers.org> wrote in message
news:e6nWpUJhFHA.1148@TK2MSFTNGP12.phx.gbl...
>I posted this question in the VB.Net newsgroup but got no response. This
> question is related to both the DotNet Framework and SQL Server, I'm
> hoping
> maybe someone can help me here.
>
> Occasionally my users will try to perform an action and be presented with
> an
> exception raised directly from SQL Server, in example:
>
> SET @err = 'a user-friendly error condition message'
> RAISERROR(@err, 16, 1, 1)
> RETURN
>
> The exception type thrown is a System.Data.SqlClient.SqlException, and its
> message property holds the exception text. Works well for the most part.
> However, a lot of times the SQL error conditions happen after a trigger is
> fired, and I tend to place informational PRINT statements in my triggers
> so
> I know what they are doing and when. So to give a simplified example to
> reproduce this condition...
>
> PRINT 'some non-user-friendly information...'
> PRINT 'more non-user-friendly information...'
> SET @err = 'a user-friendly error condition message'
> RAISERROR(@err, 16, 1, 1)
> RETURN
>
> Now the message property in the corresponding
> System.Data.SqlClient.SqlException will contain not only the error text,
> but
> (inexplicably) all the PRINT statements before it, even ones made from
> other
> stored procedures and triggers. I don't want users seeing these things as
> it
> confuses them. I can't just chop everything off after the first line
> because
> some multi-line error messages are legitimate to parse and pass to users.
> Is
> there any way I can keep PRINT messages out of the exception text, or will
> I
> have to do something ugly like prefix all PRINT statements with a
> character
> so I can parse them out?
>
> Paul
>
>
>
>
Author
9 Jul 2005 6:15 PM
Paul
D'oh. Surprised I didn't think to look at the errors collection.

Thanks!
Paul

Show quote
"Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message
news:OSWeGPKhFHA.576@tk2msftngp13.phx.gbl...
> One method is to examine the error number and display only those that are
> non-zero:
>
>     Try
>         command.ExecuteNonQuery()
>     Catch ex As SqlException
>         DisplayExceptionMessage(ex)
>     End Try
>
>     Private Sub DisplayExceptionMessage(ByVal ex As SqlException)
>         Dim swErrorMessages As New StringWriter
>         Dim sqlError As SqlError
>         For Each sqlError In ex.Errors
>             If sqlError.Number > 0 Then
>                 swErrorMessages.WriteLine(sqlError.Message)
>             End If
>         Next
>         MessageBox.Show(swErrorMessages.ToString())
>         swErrorMessages.Close()
>     End Sub
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP

AddThis Social Bookmark Button