How do I pass @attackPercentage
back to my VB procedure and have it read by the reader? I keep getting an error:
System.IndexOutOfRangeException: attackPercentage
Code:
ALTER PROCEDURE [dbo].[SelectPlayersRating]
@playerAccountID uniqueidentifier,
@raterAccountID uniqueidentifier
AS
BEGIN
DECLARE @attackPercentage INT
SELECT attack, safety, consistency
FROM tblRatings
WHERE @playerAccountID = playerAccountID
AND @raterAccountID = raterAccountID
SET @attackPercentage = '99' --Test Value
RETURN @attackPercentage
END
VB.NET code:
Dim DBConnect3 As New DBConn
Using db As DbConnection = DBConnect3.Conn("DBConnectionString")
Dim cmd As SqlCommand = DBConnect3.Command(db, "SelectPlayersRating")
cmd.Parameters.Add(New SqlParameter("playerAccountID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = Guid.Parse(Request.QueryString("aID"))
cmd.Parameters.Add(New SqlParameter("raterAccountID", SqlDbType.Uniqueidentifier, ParameterDirection.Input)).Value = acc.accountID
db.Open()
Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While DR.Read
ddlAttack.SelectedValue = DR("attack")
ddlSafety.SelectedValue = DR("safety")
ddlConsistency.SelectedValue = DR("consistency")
session("test") = DR("attckPercentage")
End While
DR.Close()
DR = Nothing
cmd.Dispose()
cmd = Nothing
db.Dispose()
db.Close()
End Using
3条答案
按热度按时间a64a0gku1#
Based on your stored procedure, your query can return multiple results, hence your While DR.Read statement.
However, your value for attackPercentage will return one value every time the stored procedure is returned. So you can use an Output Parameter to extract the value from the stored procedure:
So now, how do you get the return value? Just add an output parameter to your parameter collection:
After your call to ExecuteReader, you can simply say:
session("test") = cmd.Parameters("attackPercentage").Value
wvt8vs2t2#
I removed the return value.
This is apparently a web application. I happen to be in WinForms at the moment but this would be same in a web app, just a different events signature. Just send the parameter values to the function and then use the returned datatable.
I am not sure why you are using a DbConnection for Sql Server. Don't get a connection from somewhere else. A connection needs to be closed and disposed so use a local variable.
o2g1uqev3#
to start with if you remove the quotes where you set attackPercentage to 99 it should work. It seems pointless and less of what your trying to do but that is what is causing an exception to be thrown. I think some one answered how you pass the parameters to your query, however...im fairly new to VB so im still working on it.