Link to home
Start Free TrialLog in
Avatar of VBBRett
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").ToString'.  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("Tables")

                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").ToString
                    i = i + 1

                    MsgBox(Excelsheets(i))
                End While

            End Using

        End if


    End Sub
Avatar of g_johnson
g_johnson
Flag of United States of America image

the nullreferenceexception error can be tough to trace.  It is typically an unitialized variable and not always in the routine that reports the error.

Put a try .. catch around this and trap ex.Message and see if you get more information.

I don't see anything wrong ...
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial