NevSoFly
asked on
Problem getting ParaArray function to work
My function attempts to store arguements in a ParamArray to be used to execute a stored procedure in an SQL Server 2000 DB. My problem is that VB.net give an error saying that I can't convert a one dimensional array of string to a one dimensional array of System.Data.SqlClient.SqlP arameter. This is confusing me because I am only modifying a function that I already have and it works perfectly.
Original Code:
Usage example:
Dim dtserver As DataTable = GetServerUpdate(Table,StoredProc).Copy
Public Function GetServerUpdate(ByVal Field As Date, ByVal StoredProcName As String) As DataTable
Dim Parms(0) As SqlParameter
Parms(0) = New SqlParameter("@DateStamp", Field)
Return ExecStoredProc(StoredProcName, Parms)
End Function
Private Function ExecStoredProc(ByVal ProcedureName As String, ByVal Parms As SqlParameter()) As DataTable
'TODO: Add daSQL.FillSchema(dtSQL) after daSQL.Fill(dtSQL).
Dim dtSQL As New DataTable
Dim daSQL As New SqlDataAdapter
Dim cmdSQLCommand As New SqlCommand
' Configure the SqlCommand object
With cmdSQLCommand
.Connection = SQLConn
.CommandType = CommandType.StoredProcedure 'Set type to StoredProcedure
.CommandText = ProcedureName 'Specify stored procedure to run
' Clear any previous parameters from the Command object
Call .Parameters.Clear()
' Loop through parmameter collection adding parameters to the command object
If Not (Parms Is Nothing) Then
For Each sqlParm As SqlParameter In Parms
cmdSQLCommand.Parameters.Add(sqlParm)
Next
End If
End With
' Configure Adapter to use newly created command object and fill the dataset.
daSQL.SelectCommand = cmdSQLCommand
daSQL.Fill(dtSQL)
Return dtSQL
End Function
Modified code:
Useage example:
Dim dtserver As DataTable = GetServerUpdat(StoredProc, "variable:value").Copy
Public Function GetServerUpdate(ByVal StoredProcName As String, ByVal ParamArray ProcItem() As Object) As DataTable
Dim Parms() As System.Data.SqlClient.SqlParameter
For Each item As Object In ProcItem
Dim i As Integer
Dim ProcVariable As String = item.ToString.Substring(0, item.ToString.IndexOf(":"))
Dim ProcValue As Object = item.ToString.Substring(item.ToString.IndexOf(":"))
Parms(i) = New System.Data.SqlClient.SqlParameter(ProcVariable, ProcValue)
i = +1
Next
Return ExecStoredProc(StoredProcName, Parms) '***error happens here on Params!
End Function
Private Function ExecStoredProc is the same as the original.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
ASKER
'Private Function ExecStoredProc is the same as the original.' was not a true statement. it was actually
'Private Function ExecStoredProc(ByVal StoredProcName As String, ByVal Parms As String) As DataTable'
I changed it to the below and my errors disappeared.
'Private Function ExecStoredProc(ByVal StoredProcName As String, ByVal Parms As System.Data.SqlClient.SqlP
I also combined the two functions into one. It works when the stored procedure doesn't need an argument but when an argument is needed it returns the columns but not any records.
please comment as I have no idea what I missed.
Open in new window