I HAVE THIS ERRORS WHEN I TRY TO RUN MY CODE in visual studio 2012:When i press save button:-------------------------1. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dllAdditional information: ExecuteReader requires an open and available Connection. The connection's current state is closed.When i press show all button:--------------------------------2. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dllAdditional information: The 'Microsoft.ACE.OLEBD.12.0' provider is not registered on the local machine.The code i run:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OleDb;namespace WindowsFormsApplication2{public partial class Form1 : Form{OleDbConnection vcon=new OleDbConnection(@"Provider=Microsoft.ACE.OLEBD.12.0; Data source=C:\Users\user\Desktop\DBMS.accdb");public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){vcon.Open();}private void button1_Click(object sender, EventArgs e){this.Close();}private void button3_Click(object sender, EventArgs e){string vsql = string.Format("insert into PartA values ((0),(1),(2),(3),(4))",textBox1.Text,int.Parse(textBox2.Text),textBox3.Text,textBox4.Text,textBox5.Text);OleDbCommand vcom = new OleDbCommand(vsql, vcon);vcom.ExecuteReader();MessageBox.Show("Data stored succesfully");vcom.Dispose();}private void button4_Click(object sender, EventArgs e){string vsql = "Select * from partA ";OleDbCommand vcom = new OleDbCommand(vsql, vcon);DataSet vds = new DataSet();OleDbDataAdapter vda = new OleDbDataAdapter(vcom);vda.Fill(vds, "res");dataGridView1.DataSource = vds.Tables["res"];vda.Dispose();vcom.Dispose();}What is the solution about this error??
Loading
Upamanyu Roy ChoudhuryPosted Feb 23, 2013, 1:04 PM
Do you have "using System.Data.OleDb" at the top of your project.class?
If so then drop the DLL from the project reference and then again add it.
Keep me posted
user demPosted Feb 20, 2013, 5:20 PM
when i write using System.Data.OleDb.dll; an error message appear:
The type or namespace name 'dll' does not exists in namespase 'system.Data.OleDb' (are you missing an assemply reference?)
Also i add this code but i have the same error when i try to run my code
private OleDbConnection vcon;
public Form1()
{
InitializeComponent();
vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Desktop\DBMS.accdb");
}
Upamanyu Roy ChoudhuryPosted Feb 20, 2013, 4:42 AM
The connection string depends upon the version of access your are using.
The folllowing link shows the way connection strings can be formed using access 2007
http://www.connectionstrings.com/access-2007
Let me know if it solves the issue and if you find this post as useful mark it as an answer
Upamanyu Roy ChoudhuryPosted Feb 20, 2013, 4:33 AM
This is due to the mismatch of the provider , the connection string you are providing has to be accurte describing eveything like the Provide version, datasource,username or password (if any)
The classes you can use to connect your application to a Microsoft Access database are stored in the System.Data.OleDb namespace. To use these classes, besides including this namespace in the file where needed, you should also import the System.Data.OleDb.dll library in your project.
You have to provide the necessary informations to the Connection String.
connetionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = yourdatabasename.mdb;"
The connection string is used to specify how the connection would be performed. It is a string made of different sections. Each section is created using the formula Argument=Value. The sections are separated by semi-colons. This would produce:
"Argument1=Value;Argument=Value2;Argument_n=Value_n;"
Everything in this string is case-insensitive. If you had declared an OleDbConnection variable using the default constructor, to create the connection string, you can declare a String variable, "fill it up" with the necessary information, and assign it to the OleDbConnection.ConnectionString property.
So the provider 'Microsoft.ACE.OLEBD.12.0' is not present in your machine. Select the proper one and then your connection string will be accurate enough to connect to the DB.
Let me know if it solves your porblem
user demPosted Feb 19, 2013, 6:25 PM
when i press save or show all button:
2. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The 'Microsoft.ACE.OLEBD.12.0' provider is not registered on the local machine.
Upamanyu Roy ChoudhuryPosted Feb 19, 2013, 2:48 PM
It is just beacuse when you are trying to use the connection it is already closed,we cannot use a closed connection.
Instead of opening the connection in Load event in the button's click event write the following piece of code before the line where you are associating the command object and connection object.
if (vcon.State == ConnectionState.Closed)
{
vcon.Open();
}
OleDbCommand vcom = new OleDbCommand(vsql, vcon);
If you find this post as useful please mark it as an answer