CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2006
    Posts
    197

    Trouble passing parameters to array of sqlparameters

    I have this code
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Trouble passing parameters to array of sqlparameters

    Sorry, but I have this in VB6, but it should help:

    Code:
    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2006
    Posts
    197

    Re: Trouble passing parameters to array of sqlparameters

    Thanks dglienna, I was trying to pass the parameters inline...now it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured