Home / Learn programming language / MS access connection closed (Invalid Operation connection closed)

MS access connection closed (Invalid Operation connection closed)

Most times there are different reasons to why we cannot connect to our database but most often the reasons behind it are little system changes we ought to make but forget or don’t even know they can cause it.

When you get an error like Invalid Operation connection closed oledbconnection (access connection closed) i.e (you are using .mdb/accdb) is as a result of connection string being closed. It means you are not even connected to your database at all, so don’t even try to start dismantling your database tables and columns. Your first step to solving this problem is ensuring your database exists. And secondly ensure your connection strings are ok. Please click on this article how to connect to access database

Please note: Test your connection string by writing putting this code into your code. It will help you to know if you are connected or not.

public void InitiateDatabaseConfiguration(String connect)

{

try

{

//Initialize SQLDB.

con = null;

con = new OleDbConnection(connect);

if (con.State == ConnectionState.Closed)

{

con.Open();

}

}

catch (Exception)

{

MessageBox.Show(“Unable to Connect to the Database.”, “”, MessageBoxButtons.OK);

}

 

}

String connect is your connection string variable you pass into this code.  Let say you have connection string as:

public String dbfile = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\royal\\Documents\\Visual Studio 2008\\Projects\\Bambam\\Bambam\\bin\\Debug\\bin\\photo.mdb”;

Create a form with a button and type this code inside the button:

InitiateDatabaseConfiguration(dbfile);

When you click the button you are trying to test if your database can be reach if not reach the catch (Exception message is printed i.e unable to connect to the database). If this is the problem you are facing. Then try this:

Go to your solution explorer or your project name and right click on it then click on properties. On the build tab change the platform target to x86 and save it. Most times .mdb database will not connect to your project with target platform any cpu. And pretty sure if nothing is wrong with your connection then it should work or maybe try other articles solutions on ms access database.

Feel free to contact me on any issue. Thanks

data-matched-content-rows-num="2" data-matched-content-columns-num="2" data-matched-content-ui-type="image_stacked"

About Joseph Sam

Check Also

angularJS framework

Why you should switch to AngularJS framework

This article is focus on why you should switch to AngularJS framework. Probably you have …

Leave a Reply

Your email address will not be published. Required fields are marked *