Search code examples
exceptionado.netvisual-studio-2013oledbexception

Error: Not a valid file name ( OleDbException )


I have a simple Access database, which contains one table. Here is it Screenshot

For my Button Load event I have this code

    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()
    MsgBox("Opened")
    con.Close()

And on con.Open() line I am getting this exception . And I can not understand what is the problem. Maybe the name "con" was the problem, but I changed it to "c" or "con1" but the same exception occurs. Can't understand the reason. Thanks for any solution

Screenshot 2


Solution

  • I don't think Provider is required here, as you already have it in the source, change...

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"
    
    con.ConnectionString = dbProvider & dbSource
    

    to...

    dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"
    
    con.ConnectionString = dbSource
    

    Take a look at ConnectionStrings.

    A better solution is to add the file to the App_Data folder instead of referencing the file from a local drive. Add the connection string to the config...

    <connectionStrings>
        <add name="AccessConnection"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|dbName"
            providerName="System.Data.OleDb" />
    </connectionStrings>