Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,541 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




Passing a datasource as a Parameter in a function

 
Reply to this topicStart new topic

Passing a datasource as a Parameter in a function

MrWobbles
21 Aug, 2008 - 08:23 AM
Post #1

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 83



Thanked: 2 times
My Contributions
Calling the Function:

CODE

               With dgView
                    .Name = headerStr & "data"
                    .Height = Me.portTabs.Height
                    .Dock = DockStyle.Fill
                    .ForeColor = Color.Black
                    .DataSource = datSRC

                    .AutoResizeColumns()
                    .AutoResizeRows()
                    AddHandler dgView.CellDoubleClick, AddressOf dgView_enterCell
                End With

                fillDataGrid(headerStr, datSRC, dgView)


CODE

Private Sub fillDataGrid(ByVal tablName As String, ByVal dataSrc As DataSet, ByVal dgv As DataGridView)
        Dim sSQL As String = ""
        Dim oConn As New OleDb.OleDbConnection("Provider=SQLOLEDB;Server=.\SQLExpress;
                                                                                    Trusted_Connection=Yes;database=" & dbName)
        dataSrc.Reset()

        Try
            oConn.Open()
            sSQL = "Select * From " & tablName
            da = New OleDb.OleDbDataAdapter(sSQL, oConn)

            printDataSource(dataSrc, tablName)

            da.Fill(dataSrc, tablName)

            dgv.DataSource = dataSrc.Tables(tablName)
            dgv.Update()
        Catch ex As Exception
            MsgBox(ex.ToString & vbCr & vbCr & vbCr & tablName & vbCr & vbCr & sSQL)
        Finally
            oConn.Close()
        End Try
    End Sub


The function itself:

CODE

Private Sub printDataSource(ByVal newDS As DataSet, ByVal tablName As String)
        Dim table As DataTable
        Dim row As DataRow
        Dim column As DataColumn
        Dim i As Integer = 0
        Dim col As String

        For Each table In newDS.Tables <----- This is the line the error occurs on when calling this function
            System.Console.WriteLine(newDS.Tables(tablName).ToString)
            System.Console.WriteLine(table.TableName.ToString)
            System.Console.WriteLine("Rows: " & table.Rows.Count().ToString())
            For Each column In table.Columns
                col = column.ColumnName.ToString & " | "
                System.Console.Write(col)
            Next
            System.Console.WriteLine()
            For Each row In table.Rows
                While i < row.ItemArray.Length
                    System.Console.Write(row.Item(i) & " | ")
                    i = i + 1
                End While
                i = 0
                System.Console.WriteLine()
            Next
        Next
    End Sub


The error message:

Object Reference not set to an instance of an object.

If I take the printDataSource() function out, then I get an error on da.Fill(dataSrc, tablName) saying that the dataSrc cannot be null. I am not sure what I am doing wrong, I know that when I pass the dataSrc to the function it has a value, but for some reason it is not recognizing it? I don't know. Any help is appreciated.
User is offlineProfile CardPM
+Quote Post

MrWobbles
RE: Passing A Datasource As A Parameter In A Function
21 Aug, 2008 - 09:31 AM
Post #2

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 83



Thanked: 2 times
My Contributions
I forgot to post this, and it is probably important: this is where the datSRC value comes from:

CODE

Private Function getDataSource(ByVal table As String, ByVal tableList As ArrayList)
        If tableList.IndexOf(table) = 0 Then
            Return pDSone
        ElseIf tableList.IndexOf(table) = 1 Then
            Return pDStwo
        ElseIf tableList.IndexOf(table) = 2 Then
            Return pDSthree
        ElseIf tableList.IndexOf(table) = 3 Then
            Return pDSfour
        ElseIf tableList.IndexOf(table) = 4 Then
            Return pDSfive
        ElseIf tableList.IndexOf(table) = 5 Then
            Return pDSsix
        ElseIf tableList.IndexOf(table) = 6 Then
            Return pDSseven
        ElseIf tableList.IndexOf(table) = 7 Then
            Return pDSeight
        ElseIf tableList.IndexOf(table) = 8 Then
            Return pDSnine
        ElseIf tableList.IndexOf(table) = 9 Then
            Return pDSten
        Else
            Return "ERROR"
        End If
    End Function


pDSone through ten are declared globally and already have been assigned datasets with this function:

CODE

Private Sub createDatasets(ByVal tableList As ArrayList, ByVal oConn As OleDb.OleDbConnection)
        Dim i As Integer = 0
        Dim count As Integer = tableList.Count - 1
        Dim sql As String

        If i = 0 And i < count And Not tableList.Item(i) = "fieldInformation" Then
            'System.Console.WriteLine(tableList.Item(i))
            sql = "SELECT * FROM " & tableList.Item(i)
            pDSone = New DataSet
            da = New OleDb.OleDbDataAdapter(sql, oConn)
            da.Fill(pDSone, tableList.Item(i))

            i = i + 1
        End If

......


It goes on like that for the other 9 objects in the array, i increments for each array object.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:32PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month