Failed to map the path of my database file

Last post 07-01-2008 4:58 AM by Mikesdotnetting. 7 replies.

Sort Posts:

  • Failed to map the path of my database file

    06-29-2008, 11:50 AM
    • Loading...
    • lespaul00
    • Joined on 04-07-2008, 8:57 PM
    • Posts 28

    Hello.  I think this should be a fairly easy question, but I am having problems.

     I am using Visual Web Developer 2008, ASP.NET 2.0 and C#.

    I have two main folders: WebSite1, and MDB.  I store my .aspx files in the WebSite1 folder, and my MS Access database in the MDB folder.

    Now, for my AccessDataSources, I have:

     

    1    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    2            DataFile="../MDB/site.mdb" SelectCommand="SELECT RegionID, RegionName
    3    FROM tblRegion
    4    ORDER BY RegionName"></asp:AccessDataSource>
    

     As you can see, my mdb file is named "site.mdb".  When I do this, I get the following error:

    Failed to map the path '/MDB/site.mdb'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Failed to map the path '/MDB/site.mdb'.

    Source Error:

    Line 20:         {
    Line 21:             DropDownList1.Items.Insert(0, new ListItem("- Select One - ","0"));
    Line 22:             DropDownList1.DataBind(); 
    Line 23:             DropDownList2.Items.Insert(0, new ListItem("- Select One - ","0"));
    Line 24:             DropDownList2.DataBind();    

    BTW, the DropDownList1 is populated from AccessDataSource1.

    When I write out the entire path to the file on my hard drive (Line 2), it works.  But, I don't want to do this, because it will cause problems when I upload to a web server. 

    Am I using the correct convention "../MDB/site.mdb"?  Or is it something else?  I tried the "~/MDB/site.mdb" already too.

     Thanks!

  • Re: Failed to map the path of my database file

    06-29-2008, 12:40 PM
    • Loading...
    • whighfield
    • Joined on 01-02-2006, 10:37 PM
    • Winterpeg, Manitoba
    • Posts 272

    If the database is outside your web sites root folder (where your default.aspx page is) you may have to set the DataFile through code.  It looks like the AccessDataSource object is not letting you recurse out of your root folder (using the ../).  What if you put the MDB folder in your root web folder does it work then?

    Here is a post describing various ways to set the DataFile through code

    - William

    Please mark the most helpful reply/replies as "Answer".

    Give my enhanced version of the PWSK a look.
    www.willyd.ca
  • Re: Failed to map the path of my database file

    06-29-2008, 1:04 PM

    Use the App_Data folder for your database.  Also, avoid using the AccessDataSource control:

    AccessDataSource, SqlDataSource and connecting to Access databases in ASP.NET: http://www.mikesdotnetting.com/Article.aspx?ArticleID=78

     

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: Failed to map the path of my database file

    06-29-2008, 6:07 PM
    • Loading...
    • lespaul00
    • Joined on 04-07-2008, 8:57 PM
    • Posts 28

    Mikesdotnetting:
    Use the App_Data folder for your database.  Also, avoid using the AccessDataSource control:

     I cannot use the App_Data folder for my database.  I'm required to use the MDB folder as stated above.  Also, I do not know what you mean to avoid using the AccessDataSource control?  Your article states that it works for .mdb files only... but that's what i'm using anyway.

    Why isn't ../MDB/site.mdb not working to locate my db file?  When I do move my MDB folder it into the same folder as my default.aspx file (just for testing purposes... ultimately, I will not be allowed to do this), I can reference it by using ~/MDB/site.mdb and it works fine.

     

  • Re: Failed to map the path of my database file

    06-29-2008, 8:14 PM
    • Loading...
    • whighfield
    • Joined on 01-02-2006, 10:37 PM
    • Winterpeg, Manitoba
    • Posts 272

    The problem is that your MDB folder is outside your web root and IIS will not let you traverse outside of the web root as it's a security issue.  You will have to do something along the lines of:

    IO.DirectoryInfo di = New IO.DirectoryInfo(Server.MapPath("."));
    string fullPath = di.Parent.FullName & "MDB\\site.mdb";
    AccessDataSource1.DataFile = fullPath;

    - William

    Please mark the most helpful reply/replies as "Answer".

    Give my enhanced version of the PWSK a look.
    www.willyd.ca
  • Re: Failed to map the path of my database file

    06-30-2008, 4:18 AM

    lespaul00:

    Mikesdotnetting:
    Use the App_Data folder for your database.  Also, avoid using the AccessDataSource control:

     I cannot use the App_Data folder for my database.  I'm required to use the MDB folder as stated above.  Also, I do not know what you mean to avoid using the AccessDataSource control?  Your article states that it works for .mdb files only... but that's what i'm using anyway.

     

    My article states that the AccessDataSource works, but also points out some serious limitations that it has. That's why I suggest to avoid using it at all.  Also, why can't you use App_Data?  Is this some kind of school/college project where you are told what you must do?

     

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: Failed to map the path of my database file

    06-30-2008, 7:28 PM
    • Loading...
    • lespaul00
    • Joined on 04-07-2008, 8:57 PM
    • Posts 28

    Mikesdotnetting:
    Also, why can't you use App_Data?  Is this some kind of school/college project where you are told what you must do?

     It's for a voluntary project i'm doing at work.  So, they have specific requirements with their dynamic content (ASP.net, C#, etc.)

     So... the folder structure is as follows:

     \\MainShare\MyShare\

    Within MyShare, there are 3 folders to use: MDB, webroot, and Temp.  I have to place my .aspx files in the webroot, and my db file in the MDB folder.

     So, my .aspx files here: \MainShare\MyShare\webroot\

    My database file here: \MainShare\MyShare\MDB\

    Also, I do not want to just reference the file by: \MainShare\MyShare\MDB\site.mdb

    The shares get swapped to different mainshares sometimes, so I want to use a notation such as ~\..\ so it will work no matter what main share it's on.

    Does my problem make more sense, or less?

    Thanks a bunch!

  • Re: Failed to map the path of my database file

    07-01-2008, 4:58 AM
    Answer

    The App_Data folder gets created by ASP.NET within the webroot specifically to house your database file.  It is configured automatically to prevent prying eyes from downloading the database, and can always be referenced using |DataDirectory| with a SqlDataSource control.  This means you can move the project from machine to machine without ever having to change the connection string to accommodate different file systems.  Read the article again more closely.  It is specifically meant to help resolve issues like yours.

     

    Regards Mike
    [MVP - ASP/ASP.NET]
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter