user9407770
user9407770

Reputation: 23

Not a valid file name error while connecting to access 2013 database via VB.NET 2010

I had to make a clean system install and I lost a project I was making. I then started it from scratch but have difficulties with connection. I get the error "Not a valid file name." The weird thing is that I am able to retrieve data for a combo box. Here are the exception details :

Interception de System.Data.OleDb.OleDbException
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Not a valid file name.
  Source=Microsoft Office Access Database Engine
  StackTrace:
       à System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       à System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       à System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       à System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       à System.Data.OleDb.OleDbConnection.Open()
       à GestBanque.Securite.rechutil() dans F:\CODEBASE\HLVL\DotNet\GestBanque\GestBanque\Securite.vb:ligne 39
  InnerException: 

Here is the faulty code :

Private Sub rechutil()
        Try
            Dim dr As OleDbDataReader
            Dim cmdselect As New OleDbCommand
            Dim sql As String
            Dim cn As String
            Dim sconnexion As OleDbConnection
            cn = "provider=Microsoft.ACE.OLEDB.12.0;Password=;User ID=Admin;Data Source=‪F:\GPI3\GestBanque.accdb"
            sconnexion = New OleDbConnection(cn)
            sconnexion.Open() ' The error is raised here
            cmdselect.Connection = sconnexion
            cmdselect.CommandType = CommandType.Text
            sql = "SELECT nom, motdepasse FROM UTILISATEUR WHERE nom=? AND motdepasse=?"
            cmdselect.CommandText = sql
            cmdselect.Parameters.Add("no", OleDbType.Char)
            cmdselect.Parameters.Add("mo", OleDbType.Char)
            cmdselect.Parameters("no").Value = cmbnom.Text
            cmdselect.Parameters("mo").Value = txtpass.Text
            dr = cmdselect.ExecuteReader
            If dr.HasRows Then
                Accueil.Show()
            Else
                MessageBox.Show("Désolé, Accès refusé, Mot de passe erronné", "Authentification", MessageBoxButtons.OK, MessageBoxIcon.Error)
                cmbnom.Text = ""
                txtpass.Text = ""
            End If
            sconnexion.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Authentification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

On the contrary, this procedure works perfectly well :

Private Sub loadcombo()
        Try
            Dim dr As OleDbDataReader
            Dim cmdselect As New OleDbCommand
            Dim sql As String
            Dim cn As String
            Dim sconnexion As OleDbConnection
            cn = "provider=Microsoft.ACE.OLEDB.12.0;Password=;User ID=Admin;Data Source=F:\GPI3\GestBanque.accdb"
            sconnexion = New OleDbConnection(cn)
            sconnexion.Open()
            cmdselect.Connection = sconnexion
            cmdselect.CommandType = CommandType.Text
            sql = "SELECT nom FROM UTILISATEUR ORDER BY nom"
            cmdselect.CommandText = sql
            dr = cmdselect.ExecuteReader
            cmbnom.Items.Clear()
            While dr.Read
                cmbnom.Items.Add(dr.GetValue(0))
            End While
            sconnexion.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Chargement des noms", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub.

Here is a screen capture : Screen capt

I've been searching for about a month but found no solution on various forums. Thanks in advance.

Upvotes: 2

Views: 3509

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123629

When faced with strange errors like this, check to see if your string contains invisible Unicode characters that may have been introduced by copying and pasting. For example, this line of VB.NET code looks perfectly benign:

Dim spec As String = "‪‪W:\test.accdb"

but if I run the code

Dim spec As String = "‪‪W:\test.accdb"
For i As Integer = 0 To 4
    Console.WriteLine(Hex(AscW(spec.Substring(i, 1))))
Next

and I see this in the console output

202A
202A
57
3A
5C

it tells me that the first two characters of the string are the invisible Unicode "left-to-right embedding" characters U+202A. Even though the string looks like a valid path specification, it really isn't.

Confirmed

Confirmed by downloading your project. Line 37 of "Securite.vb" contains the invisible Unicode character cited above (U+202A) immediately before the "F:", but line 12 does not contain that character. That's why the one Sub fails and the other one doesn't.

Upvotes: 2

Related Questions