1

I have SQL SP's that I use to populate DataTables in VB and those to fill listbox/charts etc in VB form. In VB I am trying to use OOP and thus use classes for variables. In VB I have a function with the code to pull the data using a SP, this function I want to use everytime I pull data, so with every different data request you send different inputs, parameters to the function.

I can achieve this for input parameters which are of integer data type, however I am stuck with doing the same for DataTable type. If I can I would like to use the same DataTable, but when you run the function again to fill another container(listbox, then chart etc) the first container loses it's data and is empty, since the DataTable is loaded with new data. This is ofcourse after the first dataset has been used to fill the first container. Which leads me to thinking the DataTable has a "permanent" link to the container to keep it populated?

This led me to trying to declare multiple DataTables and passing a different one for every container. Here I get the problem to declare and pass DataTable type. Any solution to this problem? Another question is, is it possible to have a DataTable class?? I use MS SQL Server 2012 & MS Visual Studio 2010.

This is how my function to fill a DataTable looks:

Public Function outputData(ByVal beg_Date, ByVal end_Date, ByVal ID1, ByVal proC, ByRef dDataTable)
    If conn.State = ConnectionState.Open Then pData.Clear()

    SQLcomm = New SqlClient.SqlCommand(proC, conn)
    SQLcomm.CommandType = CommandType.StoredProcedure
    SQLcomm.Parameters.AddWithValue("@begDate", SqlDbType.Date).Value = beg_Date
    SQLcomm.Parameters.AddWithValue("@endDate", SqlDbType.Date).Value = end_Date
    SQLcomm.Parameters.AddWithValue("@ID1", SqlDbType.Int).Value = ID1


    aDapter.SelectCommand = SQLcomm
    aDapter.Fill(dDataTable)

    Return pData

End Function

proC is the SQL procedure parameter, dDataTable the DataTable parameter. Any tips will be appreciated.

Thank you,

3
  • 1
    pData doesn't seem to be doing anything.
    – LarsTech
    Sep 19, 2013 at 15:02
  • 1
    you are filling dDataTable with new data and returning pData that is empty Sep 19, 2013 at 15:04
  • apologies, in my code example dDataTable should replcae pData
    – Allstar
    Sep 19, 2013 at 18:52

1 Answer 1

2

You can make your life a lot easier by simply returning a new DataTable from this function. Consider this:

Public Function outputData(ByVal beg_Date, ByVal end_Date, ByVal ID1, ByVal proC)
    SQLcomm = New SqlClient.SqlCommand(proC, conn)
    SQLcomm.CommandType = CommandType.StoredProcedure
    SQLcomm.Parameters.AddWithValue("@begDate", SqlDbType.Date).Value = beg_Date
    SQLcomm.Parameters.AddWithValue("@endDate", SqlDbType.Date).Value = end_Date
    SQLcomm.Parameters.AddWithValue("@ID1", SqlDbType.Int).Value = ID1

    Dim dt As New DataTable()
    aDapter.SelectCommand = SQLcomm
    aDapter.Fill(dt)

    Return dt
End Function

You don't reuse DataTable objects between consumers, because yes, they are reference types so when you change them it's over. Now, if you needed to refresh a DataTable you were storing somewhere else with a new query, now all you have to do is:

myCurrentDataTable = outputData(...)

Finally, when using the SqlConnection, SqlDataAdapter, and SqlCommand classes - do not share them. Create new ones every single time you need them and wrap them in a Using statement, like this:

Using conn As New SqlConnection(cString)
    Using aDapter As New SqlDataAdapter(...)
        ' and so on
    End Using
End Using
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.