FIX: Cannot use a leading .. to exit above the top directory.

January 7th, 2009 by CertGuard


My braindump on the three solutions I found for “Cannot use a leading .. to exit above the top directory.”
Whether you’re a Classic ASP programmer or an ASP.NET developer then I’m sure you know that maintaining your Access Databases above the ROOT (i.e. outside of ‘webroot’) has it’s perks. The number one perk is that it is a secure way to keep your databases from being accessible to the public.

If you’re a Classic ASP Programmer exploring the ASP.NET realm, you’ll soon find out that you cannot use the old standard Server.MapPath(”../MyAccessDB.MDB”) to get to your once available database.

In my search for a fix to “Cannot use a leading .. to exit above the top directory.” I have found three viable solutions to this problem. And NONE of them required the changing of the “ParentPaths” setting. ParentPaths can be enabled or disable, it does not matter.


Because the solutions I found did not explain the resolutions very well, I’m going to attempt to make this as easy as possible.

FIX #1 - Move the Database to a folder within the root
I don’t like this solution. As I stated earlier, there is a perk to keeping the DB ouside the root…SECURITY! But if you’re not worried about security, or you don’t care that your DB can be downloaded by anyone that knows it’s there, go for it.


FIX #2 - Draw the Physical Path to the DB
Although this works (AND keeps your DB out of the hands of the Public) there are always unforseen problems with hardcoding paths to anything within your website.

But, if you really want to, you can do something along these lines:

Let’s assume your website is located at “c:\inetPub\wwwroot\mysite” and your DB is kept in a folder that is outside the root (i.e. “c:\inetPub\db\MyAccessDB.mdb”).Your hardcoded connection string should look like this:

dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=c:\Inetpub\db\MyAccessDB.mdb")

FIX #3 - Back out of the Webroot with a simple workaround
Remember that I stated you could not use the traditional Server.MapPath(”../MyAccessDB.MDB”) to access your database? I wasn’t lying, but you can fudge it a little. This is probably the best solution to the problem, as it allows you to access the DB and there is no hardcoding needed, which leaves your DB nice and secure. What you’re going to do is send the path into a folder (any folder that is located in your webroot), then you’re going to backout until you’ve reached the folder that houses your database. The only downside to this is that you CANNOT delete that folder, so choose a folder that you KNOW is not going anywhere.

The following is an example of the solution:

Server.MapPath("/bin/") & "..\..\MyAccessDB.MDB"

Your new connection string should look something like this:

dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.MapPath("/bin/") & "..\..\db\MyAccessDB.mdb")

Alternatively, you can set that last part into a String variable and provide that wherever you need to connect to the db.

Dim strDBConn as String = server.MapPath("/bin/") & "..\..\db\MyAccessDB.mdb"
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & strDBConn)

Additionally, if you use more than one Access Database, you can create a subroutine and provide it with the Name of the Database, such as:

Public Sub DBConn(sDB as String)
    Dim strDBConn as String = server.MapPath("/bin/") & "..\..\db\" & sDB
    dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & strDBConn)
END SUB
Share CertGuard and Join the Fight Against Braindumps:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • MySpace
  • NewsVine
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis