Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am very new to C# programming. Using .NET Framework 4.5,Microsoft Access 2010 32 bit, and 64 bit OS. I need help.
I've getting an error when writing a C# code to connect with access.Please help,thankyou.
error is follows:-
System.Data.OleDb.OleDbException (0x80004005): Not a valid file name.


and my code is:-

C#
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private OleDbConnection obj = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            obj.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪C:\Users\ABHIJITH\Documents\prelims.accdb;Persist Security Info=False;";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                obj.Open();
                MessageBox.Show("CONNECTION SUCCESSFULL");
                OleDbCommand com = new OleDbCommand();
                com.Connection = obj;
                string query = "select * from test";
                com.CommandText = query;
                OleDbDataReader read = com.ExecuteReader();
                while(read.Read())
                {
                    label1.Text = read["question"].ToString();
                }
                obj.Close();
            }
            catch(Exception ex)
            { MessageBox.Show("Error\n " + ex); }
        }
    }
}



Please help anyone, why it is not getting connected. Thank you
Posted
Updated 24-Mar-18 11:03am
v2
Comments
Kornfeld Eliyahu Peter 8-Feb-15 8:01am    
The file 'C:\Users\ABHIJITH\Documents\prelims.accdb' IS there? It is an Access file?

Look at the error message: "Not a valid file name" - that's rather clear, really.

So look at your connection string, and the path to the file:
Source=‪C:\Users\ABHIJITH\Documents\prelims.accdb
Then look on your hard drive and check if the file exists at that location...
 
Share this answer
 
Comments
Ambayi Abhijith Namboodiri 9-Feb-15 12:02pm    
yes it's in correct location. i've copied the address from properties(security tab) of database
Ambayi Abhijith Namboodiri 11-Feb-15 12:10pm    
sir, please help...
same error repeats when i installed 64 bit access and database engines....
In addition to OG's answer, since you're using 32-bit Access, you also need to compile your project as 32-bit only. Go into your project Properties, Build tab, and change the Platform Target to x86.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 8-Feb-15 11:45am    
I think that it is enough to install 32 bit driver on 64 bit OS...
Dave Kreskowiak 8-Feb-15 12:48pm    
Not quite. If his project is compiled AnyCPU and running on a 64-bit machine, he can't use the 32-bit ACE driver since his code will be running 64-bit. Unless it's already installed, he'll need to install the 64-bit ACE or compile his project down to 32-bit.

But, I've heard of problems with running the 64-bit driver on systems where 32-bit Office is installed. That's why the recommendation is to force the app to 32-bit instead.
Ambayi Abhijith Namboodiri 9-Feb-15 12:04pm    
now i've changed to ms access 64 bit..
but still error occurs...
error follows:-
system.invalidoperationexception: the 'microsoft.ace.oledb.12.0' provider is not registered on local machine
Dave Kreskowiak 9-Feb-15 12:32pm    
You normally get that because you don't have the 64-bit ACE engine installed.

That's why I said it's better to change the Platform target of your app to x86.
Ambayi Abhijith Namboodiri 11-Feb-15 7:30am    
I've installed 64 bit ACE engine also from this link:-
https://www.microsoft.com/en-in/download/details.aspx?id=13255
But also getting same result:-
system.invalidoperationexception: the 'microsoft.ace.oledb.12.0' provider is not registered on local machine
Thanks..
Hi. I had the same problem today. I think the file name (your DB name) is likely a reserved or keyword. Change the file name to something else like your name or surname or a compound name. Then try again. That's how I resolved my challenge today.

Let me know if it works. Thanks.
 
Share this answer
 
Comments
CHill60 24-Mar-18 19:16pm    
I seriously doubt the OP is still trying to resolve this 3 years down the line!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900