1

Hi am new to C# and am trying to connect to .accdb access 2010 database

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { OleDbConnection connect = new OleDbConnection(); connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Web Develop\Documents\Storekeeper\storekeeper.accdb;Persist Security Info=False;"; connect.Open(); MessageBox.Show("Connection open"); } } } 

and I get this exception:

A first chance exception of type System.Data.OleDb.OleDbException occurred in System.Data.dll

The database is not in use and the path is correct what do I do?

11
  • Are you on a 32 or 64 bit machine? Commented May 3, 2013 at 17:15
  • 3
    There should be an InnerException property on the thrown exception which you can examine. It will tell you what the exact error is. msdn.microsoft.com/en-us/library/… Commented May 3, 2013 at 17:15
  • am using visual studio 2012 cant find InnerException Commented May 3, 2013 at 17:19
  • Have you installed this : microsoft.com/en-us/download/details.aspx?id=13255 Commented May 3, 2013 at 17:21
  • Ok. Here is another question. Do you have Microsoft Office installed? And is it 32bit or 64bit? The reason I'm asking is because my old job we had a FIT with getting the drivers (the link I just gave) on 64 bit O/S machines with 32 bit MS Office installed. I don't think we ever got it resolved. (Just throwing a random tip out there just in case) Commented May 3, 2013 at 17:23

4 Answers 4

5

There should be an InnerException property on the thrown exception which you can examine. It will tell you what the exact error is. To see it, you need to catch the exception, and then show the InnerException message:

private void Form1_Load(object sender, EventArgs e) { try { OleDbConnection connect = new OleDbConnection(); connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Web Develop\Documents\Storekeeper\storekeeper.accdb;Persist Security Info=False;"; connect.Open(); MessageBox.Show("Connection open"); } catch (OleDbException e) { Messagebox.Show(e.InnerException.Message); } } 

There is additional example code for capturing and displaying the errors embedded in an OleDbException at the MSDN page for OleDbException.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't think that an OleDbException will always have an inner exception. An OleDbException has the Errors property. This is a collection of OleDbError objects, and will contain more information about the error.
Error 1 A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else C:\Users\Web Develop\Documents\Storekeeper\Storekeeper\Storekeeper\Form1.cs 30 35 Storekeeper
still dont work it throws an error for the variavle named 'e'
You should change from catch (OleDbException e) to catch (OleDbException ex) and from Messagebox.Show(e.InnerException.Message); to Messagebox.Show(ex.InnerException.Message);
lol, so how to you catch your exception which be posted question above? Look like your problem depends on environment.
|
1

I think, this is simple. Since you on the office 2010, I believe you need: Microsoft.ACE.OLEDB.14.0

1 Comment

I think you should try this recommendation WITH the "x86" suggestion I made.
1

Ok. If you have Office 32 bit on a 64 bit O/S, then you're gonna have fits. Try changing the "Platform Output" to x86. Go to your project properties and find the "Build" tab. "Platform target" should be listed there.

Now, even if that works, you'll have to investigate the ramifications of that decision. But at least "you would know".

EDIT--------------

Here are your permutations. And you're gonna just have to experiment with them.

  1. The connection string, is it right or wrong. "12" vs "14" as previously mentioned. (Sorry, the link is about Excel. Try using the suggestion from "T.S.".)

  2. Office 32 bit being installed. I think if you tried to install "AccessDatabaseEngine_x64.exe" on that machine, it would give you a "Office version not right" error.

  3. So because of #2, you gotta install "AccessDatabaseEngine.exe". Which is 32 bit.

  4. The "Platform Output". Now I ~~think~~ because of #3, you need to experiment with setting it to x86.

Try putting it back to x86, and then trying the "12" vs "14" in the connection string.

EDIT-----------------

I pulled a file off the internet.

Oren.accdb from http://old.cba.ua.edu/~jomason/ac289/289AccessFiles.html

And I coded this up on my machine. And it works.

 private void button1_Click(object sender, EventArgs e) { try { using (OleDbConnection connect = new OleDbConnection()) { connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\folder1\data\Oren.accdb;Persist Security Info=False;"; connect.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = connect; cmd.CommandText = "Select * from Agreement"; StringBuilder sb = new StringBuilder(); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0].ToString()); sb.Append(string.Format("{0}, {1}", reader[0].ToString(), reader[1].ToString()) + System.Environment.NewLine); } reader.Close(); ReportMessage(sb.ToString()); } } catch (System.Data.OleDb.OleDbException lolex) { ReportException(lolex); } catch (Exception ex) { ReportException(ex); } } private void ReportException(Exception ex) { txtStatus.Text = ex.Message; } private void ReportException(System.Data.OleDb.OleDbException oleex) { StringBuilder sb = new StringBuilder(); sb.Append(oleex.ErrorCode + System.Environment.NewLine); sb.Append(oleex.Message + System.Environment.NewLine ); txtStatus.Text = sb.ToString(); } private void ReportMessage(string msg) { txtStatus.Text = msg; } 

EDIT

Can you open the file "storekeeper.accdb" in the program "Microsoft Access". It's not password protected is it?

5 Comments

it gives a different exception "A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll"
and the application closes so i set it back to any cpu
:< Yeah dude. I feel your pain. I was up against this once myself, but left the job before we got anything concrete. (Which is why I made the blog entry that one time.)....... You probably can't do this, but you may need to find a machine without Office on it, and see if you install the AccessDatabaseEngine_x64.exe version, if you can get it to work. I know, a long road to figure something out. I don't have anymore suggestions. Office 32bit is the fly in this soup I think (???) but I'm not 100% sure.
i believe you are right ill drop it and approach from different angle :) thank you for your time
Try my example.......using the file from the internet. See if that works. My Target Platform is "x86" . When I change it to "Any CPU", I get a "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine." error. (Just to show you my x86 vs x64 talk wasn't just non sense.)
0

You need put double slash on data source path on your connection string e.g. 'Data Source=C:\folder1\data\Oren.accdb;Persist Security Info=False;";'

1 Comment

Can you please update your question to use the connection string from the question (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Web Develop\Documents\Storekeeper\storekeeper.accdb;Persist Security Info=False;"), explaining what needs to be changed? That will make your answer much clearer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.