Everytime I connect my Access database into my page for some SQL statements like SELECT, INSERT, UPDATE, and DELETE - I make use of an absolute path in my data source such as Data Source = C:\MyData\grades.mdb.

But when I make use of a relative path in my data source such as ~\MyData\grades.mdb, I always receive an OleDbException error message.

Am I doing it correctly? What should be the correct code to have a relative path to the Access Database?

Just a follow up, I am using C# as my script. I make use of OleDbConnection and OleDbCommand to facilitate my SQL transactions. How can I make use of relative path in MS Access?

Server.MapPath("~\MyData\grades.mdb")

hi ebabes,
I think problem exists in your connection string.Since you are using C# as your coding language then due to Escape sequence ('\') problem your connection string is not proper.
It should be like that.

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~\\MyData\\grades.mdb");

OR

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\\MyData\\grades.mdb";

Hope this will help you.
Thanks & Regards
Dilipv

I tried the given code this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~\\MyData\\grades.mdb"); but I received an error message "The name Server does not exist in the current context".

Any remedy to this? Thanks.

Here:

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("\MyData\grades.mdb");

You need to make sure that MyData is a folder in the root directory. This will specify the server's path to the root folder, then on from there is what you specified.

Example:
Let's say your server's physical path is: H:\inetpub\root\

By saying Server.MapPath, it will grab the above line. Then anything within the parenthesis will be added along side the server.mappath:
Server.MapPath("\MyData\grades.mdb")
=
H:\inetpub\root\MyData\grades.mdb

Thanks. I don't get any problem about the directory where I put my database. I only find a problem in the word "Server" because it is not accepted by the Web Developer when I insert the code in a class.

I wonder the code above is accepted when I insert it in a click event such as button click. But when I put the code else where in my class, there I receive the message "The word server does not exist in the current context".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.