VBBRett
asked on
A Nullreferenceexception was unhandled
Here is what I am trying to do. I am trying to pull down the names of the tables located in an Excel spreadsheet from my VB.NET application. After I load the Excel spreadsheet, I want my program to be able to located the table names, or worksheets in an Excel File. I was able to see the table names in the schema viewer, but I am trying to figure out how to get those names into a string so that I can move those names into a drop down box. I am getting a NullReferenceException was unhandled error at the line wher it says 'Excelsheets(i) = tblrow("Table_Name").ToStr ing'. Can somebody help me out please? Below is my code.
Private Sub btnGetSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetSchema.Click
Dim Path As String
Dim conn_string As OleDbConnection
Dim worksheets As New System.Data.DataTable
Dim count_tables As Integer
If TextBox1.Text = "" Then
MsgBox("No spreadsheets have been loaded yet.")
Exit Sub
Else
Path = TextBox1.Text
conn_string = New OleDbConnection( _
"Provider= Microsoft.Jet.OLEDB.4.0;" & _
"Data Source =" & Path & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";")
Using (conn_string)
conn_string.Open()
worksheets = conn_string.GetSchema("Tab les")
count_tables = worksheets.Rows.Count
MsgBox(count_tables)
Dim Excelsheets() As String
Dim i As Integer = 0
Dim tblrow As DataRow
While (i < count_tables)
Excelsheets(i) = tblrow("Table_Name").ToStr ing
i = i + 1
MsgBox(Excelsheets(i))
End While
End Using
End if
End Sub
Private Sub btnGetSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetSchema.Click
Dim Path As String
Dim conn_string As OleDbConnection
Dim worksheets As New System.Data.DataTable
Dim count_tables As Integer
If TextBox1.Text = "" Then
MsgBox("No spreadsheets have been loaded yet.")
Exit Sub
Else
Path = TextBox1.Text
conn_string = New OleDbConnection( _
"Provider= Microsoft.Jet.OLEDB.4.0;" & _
"Data Source =" & Path & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";")
Using (conn_string)
conn_string.Open()
worksheets = conn_string.GetSchema("Tab
count_tables = worksheets.Rows.Count
MsgBox(count_tables)
Dim Excelsheets() As String
Dim i As Integer = 0
Dim tblrow As DataRow
While (i < count_tables)
Excelsheets(i) = tblrow("Table_Name").ToStr
i = i + 1
MsgBox(Excelsheets(i))
End While
End Using
End if
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put a try .. catch around this and trap ex.Message and see if you get more information.
I don't see anything wrong ...