Hi,
I am triggering a stored procedure from code inside a form and I am wanting to return any TSQL errors back to the form.
I have written code to do it but when I force an error in TSQL for testing, my vb.net code drops to debug mode with "SQL Exception was unhandled" error.
Here is the code I am using
TSQL
Here is the code that calls the Stored procedure.
All I want is to return the SQL error to a message box in the VB form.
Thanks
I am triggering a stored procedure from code inside a form and I am wanting to return any TSQL errors back to the form.
I have written code to do it but when I force an error in TSQL for testing, my vb.net code drops to debug mode with "SQL Exception was unhandled" error.
Here is the code I am using
TSQL
VB.NET:
ALTER PROCEDURE dbo.GetKeySp
(
@KeyID int
)
AS
DECLARE @KeyValue int
--Key 1 - Expense Group ID
--Key 2 - Expense User ID
--Key 3 - Expense Type ID
Select @KeyValue = KeyValue
FROM Keys
WHERE KeyID = @KeyID
UPDATE Keys
SET KeyValue = KeyValue + 1
WHERE KeyID = @KeyID
SET NOCOUNT ON
--Force Error For Testing
RAISERROR ('Error raised in TRY block.', -- Message text.
16, -- Severity.
1 -- State.
)
IF @@Error <> 0
RETURN @@Error
ELSE
RETURN @KeyValue
Here is the code that calls the Stored procedure.
VB.NET:
Private Function GetExpenseTypeKey() As Integer
'This will be used to generate the Expense Type Key.
Dim SQLComm As SqlCommand
SQLComm = New SqlCommand("GetKeySp", PhantomConn)
SQLComm.CommandType = CommandType.StoredProcedure
'3 is the ID for the ExpenseType Key.
SQLComm.Parameters.Add(New SqlParameter("@KeyID", 3))
SQLComm.Parameters.Add("@Return", SqlDbType.Int)
SQLComm.Parameters("@Return").Direction = ParameterDirection.ReturnValue
PhantomConn.Open()
SQLComm.ExecuteNonQuery()
PhantomConn.Close()
If IsNumeric(SQLComm.Parameters("@Return").Value) Then
'we have the key
Return SQLComm.Parameters("@Return").Value
Else
'TSQL Error
'Show Error
MsgBox(SQLComm.Parameters("@Return").Value, MsgBoxStyle.Critical)
Return 0
End If
End Function
All I want is to return the SQL error to a message box in the VB form.
Thanks