Hi,
how do I get the return value and the raiserror message text from a
sql stored procedure?
i have the following stored procedure: (dont worry about the german
variable names)
CREATE PROCEDURE [dbo].[usr_Get_Mitarbeitertyp](
@Mitarbeitertyp_ID UNIQUEIDENTIFIER = NULL
)
AS
DECLARE @Exist INT
DECLARE @Fehlertext NVARCHAR(250)
-- return only the specified dataset
IF @Mitarbeitertyp_ID <> NULL
BEGIN
SET @Exist = (SELECT COUNT (*)
FROM [dbo].[tbl_Mitarbeitertyp]
WHERE UID_Mitarbeitertyp_ID = @Mitarbeitertyp_ID)
-- dataset exists
IF @Exist = 1
BEGIN
SELECT tbl_Mitarbeitertyp.UID_Mitarbeitertyp_ID AS MitarbeitertypID,
tbl_Mitarbeitertyp.NVC_Mitarbeitertyp AS Mitarbeitertyp
FROM [dbo].[tbl_Mitarbeitertyp]
WHERE UID_Mitarbeitertyp_ID = @Mitarbeitertyp_ID
RETURN 1
END
-- dataset does not exist
ELSE
BEGIN
EXEC @Fehlertext = usr_Get_Fehler 3
RAISERROR (@Fehlertext,16,1)
RETURN 3
END
END
ELSE
BEGIN
SELECT tbl_Mitarbeitertyp.UID_Mitarbeitertyp_ID AS MitarbeitertypID,
tbl_Mitarbeitertyp.NVC_Mitarbeitertyp AS Mitarbeitertyp
FROM [dbo].[tbl_Mitarbeitertyp]
RETURN 1
END
GO
i also have a stored procedure that gets the error message from a
seperate table which is very simple:
CREATE PROCEDURE [dbo].[usr_Get_Fehler] (
@Fehler INT
)
AS
SELECT NVC_Fehlertext FROM tbl_Fehler WHERE I_Fehler_ID = @Fehler
RETURN 1
GO
when i test it in query analyser all works fine but i have hoped to
get the message as exception in my asp.net web application.
as you can see i used severity of 16 what seems to be a correct value.
(many posts before have this as main error cause)
all worked fine, updating worked fine, deleting and inserting worked
fine. only if an error occured i want to handle it as i handle it in
my stored procedure.
my code in asp looks like this: (in a click event)
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim cmdUpdMitarbeitertyp As SqlClient.SqlCommand = _
New SqlClient.SqlCommand("usr_InsertUpdate_Mitarbeitertyp",
SqlConnection1)
cmdUpdMitarbeitertyp.CommandType = CommandType.StoredProcedure
SqlConnection1.Open() -> initialised in pageload event
Dim paramMitarbeitertyp As New
SqlClient.SqlParameter("@Mitarbeitertyp", _
SqlDbType.VarChar)
paramMitarbeitertyp.Direction = ParameterDirection.Input
paramMitarbeitertyp.Value = txtMitarbeitertyp.Text
cmdUpdMitarbeitertyp.Parameters.Add(paramMitarbeitertyp)
Dim strMitarbeitertypID As String
strMitarbeitertypID = Request.QueryString("id")
If Not strMitarbeitertypID = "" Then
Dim paramMitarbeitertypID As New
SqlClient.SqlParameter("@Mitarbeitertyp_ID", _
SqlDbType.UniqueIdentifier)
paramMitarbeitertypID.Direction = ParameterDirection.Input
paramMitarbeitertypID.Value = strMitarbeitertypID
paramMitarbeitertypID.Value =
System.Data.SqlTypes.SqlGuid.Parse(strMitarbeitertypID)
cmdUpdMitarbeitertyp.Parameters.Add(paramMitarbeitertypID)
End If
If Page.IsValid Then
Try
cmdUpdMitarbeitertyp.ExecuteNonQuery()
Catch Exp As SqlClient.SqlException
Label2.Text = Exp.Message & " ( " & Exp.Number & " )"
Finally
SqlConnection1.Close()
End Try
End If
End Sub
but i only get zero as message and 50000 as number, doesnt matter what
kind of error.
i have expencted that 3 is my exception number and the message from my
error table is in the message property of exception object
i also have tried to use the sqlerror-objekt and loop over it but
nothing.
maby i am in the wrong direction but my goal is to get the error
message that is trown by RAISERROR in stored procedure in asp.net when
the error occured. if not possible or something........ ;-)
it also would help me to get only the return value which is also the
id in my error table so that I fire another stored procedure against
it to get the message.
HELP !!!!!!!!!!!! and thanks in advance.
Andy
how do I get the return value and the raiserror message text from a
sql stored procedure?
i have the following stored procedure: (dont worry about the german
variable names)
CREATE PROCEDURE [dbo].[usr_Get_Mitarbeitertyp](
@Mitarbeitertyp_ID UNIQUEIDENTIFIER = NULL
)
AS
DECLARE @Exist INT
DECLARE @Fehlertext NVARCHAR(250)
-- return only the specified dataset
IF @Mitarbeitertyp_ID <> NULL
BEGIN
SET @Exist = (SELECT COUNT (*)
FROM [dbo].[tbl_Mitarbeitertyp]
WHERE UID_Mitarbeitertyp_ID = @Mitarbeitertyp_ID)
-- dataset exists
IF @Exist = 1
BEGIN
SELECT tbl_Mitarbeitertyp.UID_Mitarbeitertyp_ID AS MitarbeitertypID,
tbl_Mitarbeitertyp.NVC_Mitarbeitertyp AS Mitarbeitertyp
FROM [dbo].[tbl_Mitarbeitertyp]
WHERE UID_Mitarbeitertyp_ID = @Mitarbeitertyp_ID
RETURN 1
END
-- dataset does not exist
ELSE
BEGIN
EXEC @Fehlertext = usr_Get_Fehler 3
RAISERROR (@Fehlertext,16,1)
RETURN 3
END
END
ELSE
BEGIN
SELECT tbl_Mitarbeitertyp.UID_Mitarbeitertyp_ID AS MitarbeitertypID,
tbl_Mitarbeitertyp.NVC_Mitarbeitertyp AS Mitarbeitertyp
FROM [dbo].[tbl_Mitarbeitertyp]
RETURN 1
END
GO
i also have a stored procedure that gets the error message from a
seperate table which is very simple:
CREATE PROCEDURE [dbo].[usr_Get_Fehler] (
@Fehler INT
)
AS
SELECT NVC_Fehlertext FROM tbl_Fehler WHERE I_Fehler_ID = @Fehler
RETURN 1
GO
when i test it in query analyser all works fine but i have hoped to
get the message as exception in my asp.net web application.
as you can see i used severity of 16 what seems to be a correct value.
(many posts before have this as main error cause)
all worked fine, updating worked fine, deleting and inserting worked
fine. only if an error occured i want to handle it as i handle it in
my stored procedure.
my code in asp looks like this: (in a click event)
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim cmdUpdMitarbeitertyp As SqlClient.SqlCommand = _
New SqlClient.SqlCommand("usr_InsertUpdate_Mitarbeitertyp",
SqlConnection1)
cmdUpdMitarbeitertyp.CommandType = CommandType.StoredProcedure
SqlConnection1.Open() -> initialised in pageload event
Dim paramMitarbeitertyp As New
SqlClient.SqlParameter("@Mitarbeitertyp", _
SqlDbType.VarChar)
paramMitarbeitertyp.Direction = ParameterDirection.Input
paramMitarbeitertyp.Value = txtMitarbeitertyp.Text
cmdUpdMitarbeitertyp.Parameters.Add(paramMitarbeitertyp)
Dim strMitarbeitertypID As String
strMitarbeitertypID = Request.QueryString("id")
If Not strMitarbeitertypID = "" Then
Dim paramMitarbeitertypID As New
SqlClient.SqlParameter("@Mitarbeitertyp_ID", _
SqlDbType.UniqueIdentifier)
paramMitarbeitertypID.Direction = ParameterDirection.Input
paramMitarbeitertypID.Value = strMitarbeitertypID
paramMitarbeitertypID.Value =
System.Data.SqlTypes.SqlGuid.Parse(strMitarbeitertypID)
cmdUpdMitarbeitertyp.Parameters.Add(paramMitarbeitertypID)
End If
If Page.IsValid Then
Try
cmdUpdMitarbeitertyp.ExecuteNonQuery()
Catch Exp As SqlClient.SqlException
Label2.Text = Exp.Message & " ( " & Exp.Number & " )"
Finally
SqlConnection1.Close()
End Try
End If
End Sub
but i only get zero as message and 50000 as number, doesnt matter what
kind of error.
i have expencted that 3 is my exception number and the message from my
error table is in the message property of exception object
i also have tried to use the sqlerror-objekt and loop over it but
nothing.
maby i am in the wrong direction but my goal is to get the error
message that is trown by RAISERROR in stored procedure in asp.net when
the error occured. if not possible or something........ ;-)
it also would help me to get only the return value which is also the
id in my error table so that I fire another stored procedure against
it to get the message.
HELP !!!!!!!!!!!! and thanks in advance.
Andy