Link to home
Start Free TrialLog in
Avatar of Ryan Smith
Ryan SmithFlag for United States of America

asked on

VB.net error "OleDbException was unhandled" When connected to Databse.

Hello,

I'm trying to write a vb.net application and I'm getting an annoying "OleDbException was unhandled" error when my application connects to my database. The database path is fine and all the data is there. this was working a few days ago but I'm not sure why it stopped.  Please see my code below.. Any help would be greatly appreciated.

Imports System.Data.OleDb

Public Class Form1

    Private Function CreateConnString(ByVal Str As String) As String
                Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Str + ";Extended Properties=""Excel 8.0;HDR=YES;"""

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Conn As New OleDbConnection
        Dim Comm As New OleDbCommand
        Dim DReader As OleDbDataReader
        Dim Str As String = ""
        Dim Ename = EmpName.Text
        Conn.ConnectionString = CreateConnString("c:\Data\TimeData.xlsx")
        Conn.Open()
        Comm.Connection = Conn
        Comm.CommandText = "SELECT * FROM [Einfo$] where Ename= """ & Ename & """"
        DReader = Comm.ExecuteReader(CommandBehavior.CloseConnection)

        Try
            While (DReader.Read)
                TextBox4.Text = DReader.Item(1).ToString
                TextBox5.Text = DReader.Item(2).ToString
                TextBox6.Text = DReader.Item(3).ToString
                MsgBox(DReader.Item(4).ToString)
                TextBox3.Text = EmpName.Text
            End While
        Finally
            DReader.Close()
        End Try
    End Sub

End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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
Avatar of Ryan Smith

ASKER

@JamesBurger,

The message i got is "External table is not in the excpected format" What does this mean? My execl database is in the right format...
@JamesBurger,

With that message i was able to google the error. I got it working... I got the answer from here.

http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format

Thanks for your help!!!