Catch SQL Server's RAISERROR message

G

Guest

Hello everyone,

I have a HUGE database, with very complex stored procedures that handle every possible error and the passed it to an ASP site with RAISERROR.

Now I need to use that same database on an ASP.NET site, but all I can do is:

Try
Common.ExecuteNonQuery(QueryString, Common.Connection)
Catch Ex As SqlException
Result.Text = Ex.Message
Finally
Common.Connection.Close()
End Try

I really NEED to have the RAISERROR message... how can I do this?
 
A

avnrao

trap SQLException and log the same.

System.Exception innerException;
innerException = sqlE.InnerException;
while (innerException != null)
{
//logerror here
innerException = innerException.InnerException;
}
and use throw to raise the same error.

Av.
 
G

Guest

Sorry, I really don't understand your solution :

Could you be a little more specific

And, if possible, use VB and not C#?
 
A

avnrao

let me make my understanding clear..
there are lot of stored procs which RaiseError and you want to trap the
message returned by RaiseError rgt?
Ex.Message returns that error isnt it? and you can also loop thru to find
out the inner exceptions.

Av.
 
G

Guest

Yeah, that's what I want to do..

But by printing Ex.Message(), I get this

ERROR [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_User'. Cannot insert duplicate key in object 'User'. ERROR [01000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated.

And I need to show MY custom message, which I made by RAISERROR("That username is already taken. Please, choose another one",16,1)
 
C

CT

You can catch these messages by like this:

Public Class Test
Private Shared Sub OnInfoMessage(ByVal sender As Object, _
ByVal args As SqlInfoMessageEventArgs)
Dim objError As SqlError

' Loop through all the error messages
For Each objError In args.Errors
' Display the error properties
MessageBox.Show("The " & objError.Source & _
" has raised a warning on the " & _
objError.Server & " server. These are the properties :" & _
vbCrLf & vbCrLf & "Severity: " & objError.Class & vbCrLf & _
"Number: " & objError.Number & vbCrLf & "State: " & _
objError.State & vbCrLf & "Line: " & objError.LineNumber &
vbCrLf & _
"Procedure: " & objError.Procedure & vbCrLf & "Message: " & _
objError.Message)
Next

' Display the message and the source
MessageBox.Show("Info Message: " & args.Message & _
vbCrLf & "Info Source: " + args.Source)
End Sub

Public Sub TriggerInfoMessageEvent()
Dim cmmUserMan As SqlCommand = _
New SqlCommand("RAISERROR('This is an info message event.', 10,
1)", _
prcnnUserMan)
' Open the connection
prcnnUserMan.Open()
' Execute the T-SQL command
cmmUserMan.ExecuteNonQuery()
End Class

Alternatively, you use the AddHandler statement to catch the like this:

AddHandler connection.InfoMessage, New SqlInfoMessageEventHandler(AddressOf
OnInfoMessage)

The problem with the RAISERROR statement is that the severity must be set to
19 or higher for it to trigger a "normal" exception.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, and MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Enzo Marinelli said:
Hello everyone,

I have a HUGE database, with very complex stored procedures that handle
every possible error and the passed it to an ASP site with RAISERROR.
 
H

Hans Kesting

Enzo Marinelli said:
Hello everyone,

I have a HUGE database, with very complex stored procedures that handle every possible error and the passed it to an ASP site with RAISERROR.

Now I need to use that same database on an ASP.NET site, but all I can do is:

Try
Common.ExecuteNonQuery(QueryString, Common.Connection)
Catch Ex As SqlException
Result.Text = Ex.Message
Finally
Common.Connection.Close()
End Try

I really NEED to have the RAISERROR message... how can I do this?

SqlException has an Errors collection, with all "RAISedERRORs" that happened
in the your SP.

The Error collection containes SqlError classes, that have members like Message.


Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top