Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,

I need help in understanding what could possibly be the reason why am getting this error with my Excelconnection string, when i set HDR=Yes, i keep encountering d error below but with HDR=No, it return F1, F2, ......:

Error: Object reference not set to an instance of an object?


MIDL
Dim XConn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & UZO.FileName & ";" & " Extended Properties=Excel 8.0;Imex=2;HDR=Yes")



Kindly help out coz i need it to return d 1st row as the headers/column and not F1, F2 .....

Thanx guys in advance
Posted

That line of code can't possibly generate the error. Where is the code where the error does occur?

What version of Windows are you running? 32 or 64 bit? Which version of Office?

The first problem in the one line of code that you DID post is the Extended Properties value has to be enclosed in quotes, like this:
Dim xConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & UZO.FileName & ";Extended Properties=""Excel 8.0;Imex=2;HDR=Yes""")
 
Share this answer
 
Hi Dave Kreskowiak,

Windows 7, 32 bit, MS Office 2007 in my system but the Excel am using for this purpose is a template from Webtma, USA company designed as a database file and is MS Excel 1997-2003.

Thanx for the reply. The error doesn't point to any particular line of code instead whenever i run d program and set HDR=YES, it's just brought out d error and return blank output.

I've tried different ways, i even created a function for the connection string, but still not wotking.

My codes below:

XML
<pre lang="vb">Private _blnMixedData As Boolean = True
    Private _blnHeaders As Boolean = True</pre>



VB
Private Function ExcelConnection() As String
            Return "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & SVTest & ";" & "Extended Properties=" & Convert.ToChar(34).ToString() & "Excel 8.0;" & "HDR=YES;" & "Imex=2;" & Convert.ToChar(34).ToString()
        End Function




VB
Private Function ExcelConnection() As String
           Return "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & SVTest & ";" & "Extended Properties=" & Convert.ToChar(34).ToString() & "Excel 8.0;" & ExcelConnectionOptions() & Convert.ToChar(34).ToString()
       End Function



VB
Private Function ExcelConnectionOptions() As String
            Dim strOpts As String = ""
            If Me.MixedData = True Then
                strOpts += "Imex=2;"
            End If
            If Me.Headers = True Then
                strOpts += "HDR=Yes;"
            Else
                strOpts += "HDR=No;"
            End If
            Return strOpts
        End Function




VB
Private Sub RetrieveSheetnames()
       Try
           Me.ComboBox1.Items.Clear()

           If _exr IsNot Nothing Then
               _exr.Dispose()
               _exr = Nothing
           End If

           _exr = New ExcelReader()
           _exr.ExcelFilename = ExcelFilename
           _exr.Headers = True
           _exr.MixedData = True
           Dim sheetnames As String() = Me._exr.GetExcelSheetNames()
           Me.ComboBox1.Items.AddRange(sheetnames)
       Catch ex As Exception
           MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
       End Try
   End Sub




Your kind assistance would be highly appreciated.

Thanks alot
 
Share this answer
 
Comments
Dave Kreskowiak 22-Jul-11 13:02pm    
My bad. Your original line of code CAN produce that error. If the UZO variable in Nothing. You're trying to get the value of Filename from an instance of the UZO object (whatever it is) and UZO is actually Nothing, not an instance.
Dave Kreskowiak 22-Jul-11 13:03pm    
Instead of relying on global variables, try passing in actual parameters. THere is very little reason to rely on class-global variables.
Mcmish 22-Jul-11 13:41pm    
Thanx Dave, i apprecaites ur replies, i guess i have tried implementing that but still bringing same error.

Sorry to bother you more,I have a listview1 that contained list of column headers with checkedboxes, now i want to return the real data that belong to Listview1.checkedItems to DataGridview1 in the form of rows. kindly assist on the right syntax, pls. thanx

Below is the code that retrieved the column names to the listview:

Me.ListviewBindingSource.DataSource = _dt.Columns
For Each Column In ListviewBindingSource
ListView1.Items.Add(Column.ToString()
Next

What syntax can i use to get data from listview1.checkeditems and return it as DataRow???
Take a look at here[^] and read more about HDR and IMEX options.
Excel versions are available here[^].
 
Share this answer
 
Microsoft has admitted that the ODBC Option HDR=YES/NO will not work since the driver simply disregards it and suggests using OLEDB instead ...

https://support.microsoft.com/en-us/kb/288343
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900