whateveragain
Programmer
I'm trying to retrieve a value from a stored procedure but the SP doesn't return the correct value.
asp.net VB code:
Sub ckselections()
Dim sqlConn As New SqlConnection
Dim sqlComm As New SqlCommand
'Create a SqlParameter object to hold the output parameter value
MyID = Session.SessionID()
sqlComm.Connection = sqlConn
sqlConn.ConnectionString = Session("MyString")
sqlComm.CommandText = "ckselections" '-- specifying the command text
sqlComm.CommandType = CommandType.StoredProcedure '-- specifying the command text is a stored proc
With sqlComm
'-- adding the parameters
.Parameters.Add("@MyId", SqlDbType.VarChar, 50, Session("sessionid"))
'-- specifying the parameter directions
.Parameters("@MyId").Direction = ParameterDirection.Input
'-- assigning values to the parameters
.Parameters("@MyId").Value = MyID
End With
'-- opening the connection
sqlConn.Open()
Dim nselections As New SqlParameter("@nSelections", SqlDbType.BigInt)
nselections.Direction = ParameterDirection.Output
sqlComm.Parameters.Add(nselections)
sqlComm.Connection = sqlConn
sqlComm.ExecuteNonQuery()
'-- executing the command
'Now you can grab the output parameter's value...
Dim reader As SqlDataReader = sqlComm.ExecuteReader()
' Dim nnum As Integer = nselections.Value
'Dim nnum As Integer = Convert.ToDecimal(nselections.Value)
Dim nnum As Integer = Convert.ToInt16(nselections.Value)
'-- close the connection
If IsNumeric(nnum) Then
If nnum = 0 Then --Value of nnum is incorrect at this point
MsgBox("nnum = 0")
ElseIf nnum = 1 Then
MsgBox("nnum = 1")
ElseIf nnum = 2 Then
MsgBox("nnum = 2")
End If
Else
MsgBox("nnum is not numeric")
End If
sqlConn.Close()
End Sub
Stored Procedure:
ALTER PROCEDURE dbo.ckselections
(
@MyID varchar(55),
@nSelections bigint OUTPUT
)
AS
/* SET NOCOUNT ON */
declare @tmpselcond varchar(55) = 'tmpselcond' + ltrim(rtrim(@MyID))
set rowcount 0
exec('select * from ' + @tmpselcond + ' where selcode = 1')
set @nSelections = @@ROWCOUNT
print '@nSelections = '
print @nSelections --Value of @nSelections is correct at this point
RETURN @nSelections
asp.net VB code:
Sub ckselections()
Dim sqlConn As New SqlConnection
Dim sqlComm As New SqlCommand
'Create a SqlParameter object to hold the output parameter value
MyID = Session.SessionID()
sqlComm.Connection = sqlConn
sqlConn.ConnectionString = Session("MyString")
sqlComm.CommandText = "ckselections" '-- specifying the command text
sqlComm.CommandType = CommandType.StoredProcedure '-- specifying the command text is a stored proc
With sqlComm
'-- adding the parameters
.Parameters.Add("@MyId", SqlDbType.VarChar, 50, Session("sessionid"))
'-- specifying the parameter directions
.Parameters("@MyId").Direction = ParameterDirection.Input
'-- assigning values to the parameters
.Parameters("@MyId").Value = MyID
End With
'-- opening the connection
sqlConn.Open()
Dim nselections As New SqlParameter("@nSelections", SqlDbType.BigInt)
nselections.Direction = ParameterDirection.Output
sqlComm.Parameters.Add(nselections)
sqlComm.Connection = sqlConn
sqlComm.ExecuteNonQuery()
'-- executing the command
'Now you can grab the output parameter's value...
Dim reader As SqlDataReader = sqlComm.ExecuteReader()
' Dim nnum As Integer = nselections.Value
'Dim nnum As Integer = Convert.ToDecimal(nselections.Value)
Dim nnum As Integer = Convert.ToInt16(nselections.Value)
'-- close the connection
If IsNumeric(nnum) Then
If nnum = 0 Then --Value of nnum is incorrect at this point
MsgBox("nnum = 0")
ElseIf nnum = 1 Then
MsgBox("nnum = 1")
ElseIf nnum = 2 Then
MsgBox("nnum = 2")
End If
Else
MsgBox("nnum is not numeric")
End If
sqlConn.Close()
End Sub
Stored Procedure:
ALTER PROCEDURE dbo.ckselections
(
@MyID varchar(55),
@nSelections bigint OUTPUT
)
AS
/* SET NOCOUNT ON */
declare @tmpselcond varchar(55) = 'tmpselcond' + ltrim(rtrim(@MyID))
set rowcount 0
exec('select * from ' + @tmpselcond + ' where selcode = 1')
set @nSelections = @@ROWCOUNT
print '@nSelections = '
print @nSelections --Value of @nSelections is correct at this point
RETURN @nSelections