Oledb query returns no records


I am using a sample Oledb to query an excel file. The file exists the code runs with no errors but returns no data, can anyone spot an error in the code.


Private Const connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\joe\My Documents\driver quals-input.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim conn As New OleDbConnection(connstring)
            Dim da As New OleDbDataAdapter("Select * From [Sheet1$]", conn)
            Dim ds As DataSet = New DataSet()
            Debug.WriteLine(vbCrLf & "ContactData:" & vbCrLf & "==============")
            Dim dr As DataRow
            For Each dr In ds.Tables(0).Rows
                Debug.WriteLine(dr("Name") & " - " & dr("Mobile No") & " - " & dr("Home No"))
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I can't see anything wrong with the code. I've just tested the following code (it's in C#, but you can get the gist of it) with the attached XLS file, and the Rows array returned contains the correct data.

      string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
      @"Data Source=c:\foo.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
      OleDbConnection conn = new OleDbConnection(connString);
      OleDbDataAdapter adapter = new OleDbDataAdapter(
        "SELECT * FROM [Sheet1$]", conn);
      DataSet ds = new DataSet();
      DataRow[] rows = ds.Tables[0].Select();

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoeBo747Author Commented:
Hi oobaly
Thanks for your response, I have converted the code supplied to vb.net and attempted to query the file using the code below, I get no rows returned. I am using Excel 2007! Is this an issue(I have saved the workbook as an 2003 style workbook).

 Dim conn As OleDbConnection = New OleDbConnection(connstring)
            Dim adapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn)
            Dim ds As DataSet = New DataSet()
            If ds.Tables(0).Rows.Count > 0 Then
                Dim rows() As DataRow = ds.Tables(0).Select()
            End If

Open in new window

can you please attach some part of your excel file over here ?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Well, I don't even have Excel installed on my machine, I created the xls file using OpenOffice which allows me to save in "MS Excel 97/2000/XP", and I can't save in a newer versions to test.

I'm not convinced the version of Excel installed on the system should make a difference as you're using the MS Jet DB driver to access an Excel file.

You said "and attempted to query the file using the code below". Do you mean the sample Excel file that I provided, or your own Excel file? If it was the file I provided, I really can't see why nothing is being returned.

If you go to Control Panel -> Administrative Tools -> Data Sources (ODBC) -> Drivers, can you tell me what version of ODBCJT32.dll you're using, not that I'm entirely convinced that's the issue either.
JoeBo747Author Commented:
Hi oobaly
I am using  4.00.6305.00 dll the file I am trying to query is  both the file you supplied (foo.xls)and my own file!
JoeBo747Author Commented:
I believe I may have fallen for excels positive indexing style because when I look at the table using

Dim rows() As DataRow = ds.Tables(1).Select()

Rather than

Dim rows() As DataRow = ds.Tables(0).Select()

I return a value as expected!

Thanks for your time
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.