473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Catch SQL Server's RAISERROR message

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?
Nov 18 '05 #1
6 13742
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.

"Enzo Marinelli" <an*******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
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?

Nov 18 '05 #2
Sorry, I really don't understand your solution :

Could you be a little more specific

And, if possible, use VB and not C#?
Nov 18 '05 #3
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.

"Enzo Marinelli" <an*******@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
Sorry, I really don't understand your solution :$

Could you be a little more specific?

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

Nov 18 '05 #4
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)
Nov 18 '05 #5
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" <an*******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
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?

Nov 18 '05 #6

"Enzo Marinelli" <an*******@discussions.microsoft.com> wrote in message news:7E**********************************@microsof t.com...
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

Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jason | last post by:
I am trying to get a better understanding of when to use return (with a print statement) and when to use raiserror. * Both statements can be used with stored procedures while only return can be...
4
by: Evgeny Gopengauz | last post by:
Is there something like exception handling in T-SQL? For example, how to catch an error of convertion at this sample: CREATE PROCEDURE SP @param VARCHAR(50) AS BEGIN DELCARE @var INT -- try...
0
by: Enzo Marinelli | last post by:
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...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
2
by: davidz | last post by:
When a catchable error occurs in a try block, control is passed to the associated catch block. While I am then able to examine properties of the error using such functions as ERROR_NUMBER() in the...
2
by: Jim Devenish | last post by:
I am developing an Access front-end linked to SQL Server back-end. In the triggers I raise an error as in: RAISERROR 44447 'The record cannot be changed. RI rules require a related record in...
0
by: SQLNovice | last post by:
Hi fellow-developers, I am literally hitting a wall trying to get the syntax right for this case statement. Could someone please point out where I am going wrong? Thanks in advance :) ...
1
by: eighthman11 | last post by:
Hey everyone, Using Access 2003 and SQL Server 8.0 I have a stored procdure on SQL Server that is launched from my access database where I send the parms. The stored procedure on the server...
3
by: kctan | last post by:
Hi, I have a VB.Net system that connect to SQL Server through ODBC. When I try to raiseerror from an IF...ELSE statement, it does not triggered the exception handling. I have done some raise...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.