[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problem getting ParaArray function to work

Posted on 2008-10-14
3
Medium Priority
?
350 Views
Last Modified: 2010-04-21
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.SqlParameter.  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.

Open in new window

0
Comment
Question by:NevSoFly
[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
3 Comments
 
LVL 2

Accepted Solution

by:
wmestrom earned 2000 total points
ID: 22720621
Please recheck wether you submitted the correct code fragment here. There are a few typo's and the Parms array is uninitialized which will give a nullreference exception but other than that the code compiles fine in VS2005.
0
 

Author Comment

by:NevSoFly
ID: 22724585
Ok, I had been looking at the code way too long and did't see what was in front of me.  The problem turned out to be that I didn't submit the entire code.  In the modified code section the line
'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.SqlParameter()) As DataTable'

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.



    Private Function ExecStoredProc(ByVal StoredProcName As String, ByVal ParamArray ProcItem() As String) As DataTable
        'StoredProcName = the name of the stored procedure to be executed.
        'ProcItem = String combination of the stored procedure's argument and the argument's value in the format "@arg:value".
        'example1:  Dim dtserver As DataTable = ExecStoredProc("usp_test").Copy
        'example2:  Dim dtserver As DataTable = ExecStoredProc("usp_test", "@arg1:value1","@arg2:value2").Copy
 
        Dim Parms(0) As System.Data.SqlClient.SqlParameter
        Dim dtSQL As New DataTable
        Dim daSQL As New System.Data.SqlClient.SqlDataAdapter
        Dim cmdSQLCommand As New System.Data.SqlClient.SqlCommand
 
        For Each item As String In ProcItem
            Dim i As Integer
            Dim ProcVariable As String = item.ToString.Substring(0, item.ToString.IndexOf(":"))
            Dim ProcValue As String = item.ToString.Substring(item.ToString.IndexOf(":") + 1)
            Parms(i) = New System.Data.SqlClient.SqlParameter(ProcVariable, ProcValue)
            i = +1
        Next
 
        ' Configure the SqlCommand object
        With cmdSQLCommand
            .Connection = SQLConn
            .CommandType = CommandType.StoredProcedure 'Set type to StoredProcedure
            .CommandText = StoredProcName '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 System.Data.SqlClient.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)
        daSQL.FillSchema(dtSQL, SchemaType.Source)
 
        Return dtSQL
    End Function

Open in new window

0
 

Author Closing Comment

by:NevSoFly
ID: 31506123
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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