Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,
I use the function below. Using this code:
VB
Dim Parameters(0 To 1) As SqlParameter
Parameters(0) = New SqlParameter("@username";, txtUsername.Text)
Parameters(1) = New SqlParameter"@ret", ret)
ret =clsDataAccess.NonQuery("usp_checkLogin",Parameters,
CommandType.StoredProcedure)

I got an error in the second parameter (Parameters) in the function (ByVal ParameterValues As List(Of SqlParameter)... the error is ....
Value of type '1-dimensional array of System.Data.SqlClient.SqlParameter' cannot be converted to 'System.Collections.Generic.List(Of System.Data.SqlClient.SqlParameter)'.

What's missing in my code?

Here is the function:
VB
Public Shared Function NonQuery(ByVal CommandText As String, _
           ByVal ParameterValues As List(Of SqlParameter), _
           ByVal CommandType As CommandType) As Integer
        Dim res As Integer = 0
        Dim Parameter As SqlParameter
        Dim SqlConn As SqlConnection = Connection
        Try
            SqlConn.Open()
            SqlComm = New SqlCommand(CommandText, SqlConn)
            SqlComm.CommandTimeout = 600
            SqlComm.CommandType = CommandType
            If (ParameterValues IsNot Nothing) Then
                For Each Parameter In ParameterValues
                    SqlComm.Parameters.Add(Parameter)
                Next
            End If
            res = SqlComm.ExecuteNonQuery()
        Catch ex As Exception
            Throw ex
        Finally
            SqlConn.Close()
        End Try
        Return res
    End Function</pre>

Again, please help me..

Thanks so much in advance!
Regards,
Posted
Updated 7-Feb-11 23:30pm
v2

You need to convert your arraylist into a generic list. Have a read of the below that should point you in the right direction

Converting an arraylist into a generic list[^]
 
Share this answer
 
You are trying to pass an array to a function signature expecting a list.

SqlParmameter(0 to 1) is not the same as List(Of Sql Parameter).

Quick Fix: Change it to a list
"usp_checkLogin",Parameters.ToList,CommandType.StoredProcedure)


Better Fix: Use a list in the first place
Dim Parameters As New List(Of SqlParameter)
Parameters.Add(New SqlParameter("@username", txtUsername.Text))
Parameters.Add(New SqlParameter("@ret", ret))
 
Share this answer
 
You're missing the left parenthesis. Yours is this:

VB
Parameters(1) = New SqlParameter"@ret", ret)


It should be this:

VB
Parameters(1) = New SqlParameter("@ret", ret)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900