Error: Must Declare the Scalar Variable
Here's my code:
Public Function selectReturnsByUserId( limit As Integer, userid As String ) As DataSet
Dim sql As String = " SELECT TOP " & limit & " pr.ProductId, p.Title, p.Barcode, pr.ScanDate, pr.UserId, pr.ReturnStatus" & _
" FROM " & tablename & " pr " 开发者_Go百科& _
" INNER JOIN " & StaticValues.TABLENAME_PRODUCT & " p ON pr.ProductId = p.ProductId" & _
" WHERE pr.UserId = @UserId"
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand( sql )
cmd.Parameters.AddWithValue( "@UserId", userid )
Return _select( cmd )
End Function
Which Calls:
Protected Function _select(ByVal cmd As SqlClient.SqlCommand) As DataSet
Dim ds As New DataSet
Dim myAdapter As New System.Data.SqlClient.SqlDataAdapter(cmd.CommandText, DBConnection.getInstance().getConnection().ConnectionString)
myAdapter.Fill( ds, tablename )
Return ds
End Function
And when I try to run it, I get this error:
Must declare the scalar variable "@UserId"
On this line:
myAdapter.Fill( ds, tablename )
How can I fix that line?
Thanks
You never passed the parameter to the SqlDataAdapter.
You should change the _select
method to use the original SqlCommand
, or to copy its parameters.
精彩评论