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.