Solved

Passing a variable number of parameters to a function

Posted on 2015-01-28
2
58 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
2 Comments
 
LVL 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now