Solved

Oledb query returns no records

Posted on 2009-07-12
6
484 Views
Last Modified: 2012-06-22
Hi

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.

Rgards
JoeBo

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

        Try
 

            Dim conn As New OleDbConnection(connstring)

            Dim da As New OleDbDataAdapter("Select * From [Sheet1$]", conn)

            Dim ds As DataSet = New DataSet()

            da.Fill(ds)

            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"))

            Next

            conn.Close()
 

        Catch ex As Exception

            MsgBox(Err.Description)

        End Try

    End Sub

Open in new window

0
Comment
Question by:JoeBo747
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24834018
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();

      adapter.Fill(ds);
 

      DataRow[] rows = ds.Tables[0].Select();

Open in new window

foo.xls
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 24834078
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).
Regards
Joe  

 Dim conn As OleDbConnection = New OleDbConnection(connstring)

            Dim adapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn)
 

            Dim ds As DataSet = New DataSet()

            adapter.Fill(ds)
 

            If ds.Tables(0).Rows.Count > 0 Then

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

                MsgBox(rows(0).Item(0).ToString)

            End If

Open in new window

0
 
LVL 15

Expert Comment

by:jinal
ID: 24834224
can you please attach some part of your excel file over here ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:oobayly
ID: 24834234
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.
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 24836109
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!
Regards
Joebo
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 24836290
Hi
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
Joebo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now