Solved

Passing a variable number of parameters to a function

Posted on 2015-01-28
2
65 Views
Last Modified: 2015-01-28
Hi,

Why doesn't this work:

 For Each strline As String In my852File.Split(vbCrLf)
            Dim line852() = strline.Split(",")
            Dim parms(24) As SqlParameter
            parms(cnt) = New SqlParameter("@" + ColNames(cnt), line852(cnt))
            clsData.SQLInsert(parms)

' parms above is hightlighted with this error: Value of type '1-dimensional array of System.Data.SqlClient.SqlParameter' cannot be converted to 'System.Collections.Generic.List(Of System.Data.SqlClient.SqlParameter)'.


            cnt += 1
Next

Open in new window


sqlInsert:

 Public Function SQLInsert(ByVal ParameterValues As List(Of SqlParameter)) As Boolean
        Try
            Dim ConnectionStr As New SqlConnection(myVars.GetSQLString)
            Dim sqlCMD As New SqlCommand()
            For Each parm As SqlParameter In ParameterValues
                sqlCMD.Parameters.Add(parm)
            Next

            sqlCMD.CommandType = CommandType.StoredProcedure

            sqlCMD.ExecuteNonQuery()
            
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

Open in new window

0
Comment
Question by:prosit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40576075
It's because parms is an array, not a list (two different types of collections).  Use parms.ToList() instead:
For Each strline As String In my852File.Split(vbCrLf)
            Dim line852() = strline.Split(",")
            Dim parms(24) As SqlParameter
            parms(cnt) = New SqlParameter("@" + ColNames(cnt), line852(cnt))
            clsData.SQLInsert(parms.ToList())
            '''
            '''
            cnt += 1
Next

Open in new window

-saige-
0
 
LVL 2

Author Closing Comment

by:prosit
ID: 40576107
Perfect thanks!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question