Click to See Complete Forum and Search --> : Trouble passing parameters to array of sqlparameters


RH+
September 19th, 2009, 07:47 PM
I have this code

Public Function ExecuteDataReaderSP( _
ByVal storedProcedureName As String, _
ByVal ParamArray arrParam() As SqlParameter) As SqlDataReader

Dim reader As SqlDataReader

' Open the connection
If m_cn.State <> ConnectionState.Open Then m_cn.Open()

' Define the command
Dim cmd As New SqlCommand
cmd.Connection = m_cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = storedProcedureName

' Handle the parameters
If arrParam IsNot Nothing Then
For Each param As SqlParameter In arrParam
cmd.Parameters.Add(param)
Next
End If

' Execute the reader
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Return reader
End Function


how I pass parameters
im using ExecuteDataReaderSP("Name_Of_SP",parameter1,parameter2)

I try various ways to pass the parameters, every time it return a different error.
How is the correct way to pass those parameters?

thanks!

dglienna
September 19th, 2009, 07:54 PM
Sorry, but I have this in VB6, but it should help:

Option Explicit

Private Sub Form_Load()
ParamAdd
End Sub

Sub ParamAdd()
Dim cmdSQLInsert As ADODB.Command
Set cmdSQLInsert = New ADODB.Command

'Create the query
cmdSQLInsert.CommandText = "Insert Into Table1(ID, NAME, AGE) Values(?,?,?)"
cmdSQLInsert.CommandType = adCmdText
cmdSQLInsert.Prepared = True

'Create the parameters
'in this case we will create three parameters
'-----Param 1 (for Field ID)-------------
Dim gParam As ADODB.Parameter
Set gParam = New ADODB.Parameter
With gParam
.Name = "ID"
.Direction = adParamInput
.Type = adChar
.Size = 10
.Value = "xxxxxxxxxx"
End With
cmdSQLInsert.Parameters.Append gParam

'-----Param 2 (for Field Name)-------------
Set gParam = Nothing
Set gParam = New ADODB.Parameter
With gParam
.Name = "NAME"
.Direction = adParamInput
.Type = adVarChar
.Size = 50
.Value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
End With
cmdSQLInsert.Parameters.Append gParam

'-----Param 3 (for Field AGE)-------------
Set gParam = Nothing
Set gParam = New ADODB.Parameter
With gParam
.Name = "AGE"
.Direction = adParamInput
.Type = adChar
.Size = 2
.Value = "xx"
End With
cmdSQLInsert.Parameters.Append gParam

'Set the connection property of the command object
Set cmdSQLInsert.ActiveConnection = mySQLConnection
'pass the values that need to be inserted to specific parameters that we created above
cmdSQLInsert("ID") = txtID.Text
cmdSQLInsert("NAME") = txtID.Text
cmdSQLInsert("AGE") = txtAge.Text

'Execute the command
cmdSQLInsert.Execute
End Sub

RH+
September 20th, 2009, 12:30 PM
Thanks dglienna, I was trying to pass the parameters inline...now it works!