Code
I HAVE THIS ERRORS WHEN I TRY TO RUN MY CODE in visual studio 2012:Code
When i press save button:Code
-------------------------Code
1. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dllCode
Additional information: ExecuteReader requires an open and available Connection. The connection's current state is closed.Code
Code
When i press show all button:Code
--------------------------------Code
Code
2. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dllCode
Code
Additional information: The 'Microsoft.ACE.OLEBD.12.0' provider is not registered on the local machine.Code
Code
The code i run:Code
Code
using System;Code
using System.Collections.Generic;Code
using System.ComponentModel;Code
using System.Data;Code
using System.Drawing;Code
using System.Linq;Code
using System.Text;Code
using System.Windows.Forms;Code
using System.Data.OleDb;Code
Code
namespace WindowsFormsApplication2Code
{Code
public partial class Form1 : FormCode
{Code
Code
Code
OleDbConnection vcon=new OleDbConnection(@"Provider=Microsoft.ACE.OLEBD.12.0; Data source=C:\Users\user\Desktop\DBMS.accdb");Code
Code
Code
public Form1()Code
{Code
InitializeComponent();Code
Code
}Code
Code
private void Form1_Load(object sender, EventArgs e)Code
{Code
vcon.Open();Code
Code
Code
}Code
Code
private void button1_Click(object sender, EventArgs e)Code
{Code
this.Close();Code
}Code
Code
private void button3_Click(object sender, EventArgs e)Code
{VB.NET
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);Code
Code
OleDbCommand vcom = new OleDbCommand(vsql, vcon);Code
Code
vcom.ExecuteReader();Code
MessageBox.Show("Data stored succesfully");Code
vcom.Dispose();Code
Code
Code
}Code
private void button4_Click(object sender, EventArgs e)Code
{Code
string vsql = "Select * from partA ";Code
OleDbCommand vcom = new OleDbCommand(vsql, vcon);Code
DataSet vds = new DataSet();Code
OleDbDataAdapter vda = new OleDbDataAdapter(vcom);Code
vda.Fill(vds, "res");Code
dataGridView1.DataSource = vds.Tables["res"];Code
vda.Dispose();Code
vcom.Dispose();Code
Code
Code
}Code
Code
Code
What is the solution about this error??
Upamanyu Roy Choudhury146 reputationPosted Feb 19, 2013, 9:48 AM EST
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
user dem5 reputationPosted Feb 19, 2013, 1:25 PM EST
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 Choudhury146 reputationPosted Feb 19, 2013, 11:33 PM EST
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
Upamanyu Roy Choudhury146 reputationPosted Feb 19, 2013, 11:42 PM EST
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
user dem5 reputationPosted Feb 20, 2013, 12:20 PM EST
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 Choudhury146 reputationPosted Feb 23, 2013, 8:04 AM EST
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