Created
February 3, 2016 10:03
-
-
Save cpoDesign/f1c50a51608547816fc4 to your computer and use it in GitHub Desktop.
example to read return value from sp and output value in vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim returnValue As String = "" | |
Try | |
Using tmsCommand As New SqlCommand("dbo.getUserXml", _tmsConnection) | |
If _tmsConnection.State = ConnectionState.Closed Then _tmsConnection.Open() | |
tmsCommand.CommandType = CommandType.StoredProcedure | |
tmsCommand.Parameters.Add(New SqlParameter("@UserId", SqlDbType.Int, ParameterDirection.Input)).Value = userID | |
tmsCommand.Parameters.Add(New SqlParameter("@UserXml", SqlDbType.Xml, 1)).Direction = ParameterDirection.Output | |
Dim returnSpValue = New SqlParameter("@ReturnVal", SqlDbType.Int) | |
tmsCommand.Parameters.Add(returnSpValue).Direction = ParameterDirection.ReturnValue | |
tmsCommand.ExecuteNonQuery() | |
If Not (tmsCommand.Parameters("@UserXml").Value Is DBNull.Value) Then | |
returnValue = tmsCommand.Parameters("@UserXml").Value | |
End If | |
Dim result = returnSpValue.Value | |
If (result = -1) Then | |
returnValue = returnSpValue.Value | |
Throw New Exception("Failed to execute sp correctly") | |
End If | |
End Using | |
Catch Ex As SqlException | |
_errorMessage = Ex.Message | |
Catch Ex As Exception | |
_errorMessage = Ex.Message | |
Finally | |
End Try | |
Return returnValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment