using Server.MapPath

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default using Server.MapPath

    Hi,

    Can somebody tell me why can't I use
    string strPath = Server.MapPath(Request.ApplicationPath);
    in
    protected void Application_Start(...)

    It compiles ok but an unhandled exception occurred during the execution.
    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    I have no problem using the above code in private void Page_Load(...)

    Thanks,
    Kian


    Kian Goh Guest

  2. Similar Questions and Discussions

    1. this.Context.Server.MapPath
      can get the Server.MapPath to work in run-mode, but not in the property desinger window. It will throw an error. Try to read a xml file in the...
    2. Accessing Server.Mappath from SoapExtension
      I need to log all incoming soap messages in a file. I have follwing code in WriteInput routine of SoapExtension class Dim Server As...
    3. Server.MapPath connection string question.
      I have two sites: www.site_A.com & www.site_A_news.com Site_A is in d:\webs\site_a & uses an Access DB in d:\webs\site_a\data\db.mdb ...
    4. Server.MapPath returning wrong path
      I've got the directory f:\Company\Product set as web shared so it's got a virtual directory in the default web site on my test server's IIS. If I...
    5. Server MapPath
      Hello folks. How do I solve the problem below? when my program executes the line below : Package.WriteXml(Server.MapPath(strID + ".XML")); ...
  3. #2

    Default Re: using Server.MapPath

    There is no Request yet when your application is starting up.
    The Request object is not yet in scope.

    --
    I hope this helps,
    Steve C. Orr, MCSD
    [url]http://Steve.Orr.net[/url]



    "Kian Goh" <kiangoh2700544@hotmail.com> wrote in message
    news:ePw%23YfiUDHA.3916@tk2msftngp13.phx.gbl...
    > Hi,
    >
    > Can somebody tell me why can't I use
    > string strPath = Server.MapPath(Request.ApplicationPath);
    > in
    > protected void Application_Start(...)
    >
    > It compiles ok but an unhandled exception occurred during the execution.
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > I have no problem using the above code in private void Page_Load(...)
    >
    > Thanks,
    > Kian
    >
    >

    Steve C. Orr, MCSD Guest

  4. #3

    Default Re: using Server.MapPath

    Thanks Steve.

    I intended to implement a counter for my webpage. I store my counter value
    in a text file located in the ApplicationPath. I will read the counter from
    the file on application start up and increment the value on every new
    session created. Counter will be written on to the file at the end of
    application.

    If there is no Request yet during the application start up, is there any way
    for me to know the ApplicationPath. At the moment, I am using hard coded
    path.

    OR let me know if there is other way to implement a counter.

    Thanks,
    Kian

    ====================================
    INSIDE protected void Application_Start(...)
    ====================================
    StreamReader sr = File.OpenText("d:\\kk\\omac\\omacCounter.txt");
    Application["Counter"] =
    System.Convert.ToInt16(sr.ReadToEnd().ToString());
    ====================================



    "Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message
    news:e2dVAoiUDHA.2316@TK2MSFTNGP09.phx.gbl...
    > There is no Request yet when your application is starting up.
    > The Request object is not yet in scope.
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD
    > [url]http://Steve.Orr.net[/url]
    >
    >
    >
    > "Kian Goh" <kiangoh2700544@hotmail.com> wrote in message
    > news:ePw%23YfiUDHA.3916@tk2msftngp13.phx.gbl...
    > > Hi,
    > >
    > > Can somebody tell me why can't I use
    > > string strPath = Server.MapPath(Request.ApplicationPath);
    > > in
    > > protected void Application_Start(...)
    > >
    > > It compiles ok but an unhandled exception occurred during the execution.
    > > Exception Details: System.NullReferenceException: Object reference not
    set
    > > to an instance of an object.
    > >
    > > I have no problem using the above code in private void Page_Load(...)
    > >
    > > Thanks,
    > > Kian
    > >
    > >
    >
    >

    Kian Goh Guest

  5. #4

    Default Re: using Server.MapPath

    Well one solution would be to store the path in your web.config file.
    Then in your code you can get the path like this:
    Dim sMyPath As String = ConfigurationSettings.AppSettings("AppPath")
    Here's more info:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemconfigurationconfigurationsettingsclass appsettingstopic.asp[/url]

    But a better solution might be to user the Server.Mappath method instead. I
    believe the Server object is in scope in the Application_Start procedure.
    Here's more info:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpserverutilityclassmappathtopic.a sp[/url]

    --
    I hope this helps,
    Steve C. Orr, MCSD
    [url]http://Steve.Orr.net[/url]


    "Kian Goh" <kiangoh2700544@hotmail.com> wrote in message
    news:OLlwcziUDHA.360@TK2MSFTNGP11.phx.gbl...
    > Thanks Steve.
    >
    > I intended to implement a counter for my webpage. I store my counter value
    > in a text file located in the ApplicationPath. I will read the counter
    from
    > the file on application start up and increment the value on every new
    > session created. Counter will be written on to the file at the end of
    > application.
    >
    > If there is no Request yet during the application start up, is there any
    way
    > for me to know the ApplicationPath. At the moment, I am using hard coded
    > path.
    >
    > OR let me know if there is other way to implement a counter.
    >
    > Thanks,
    > Kian
    >
    > ====================================
    > INSIDE protected void Application_Start(...)
    > ====================================
    > StreamReader sr = File.OpenText("d:\\kk\\omac\\omacCounter.txt");
    > Application["Counter"] =
    > System.Convert.ToInt16(sr.ReadToEnd().ToString());
    > ====================================
    >
    >
    >
    > "Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message
    > news:e2dVAoiUDHA.2316@TK2MSFTNGP09.phx.gbl...
    > > There is no Request yet when your application is starting up.
    > > The Request object is not yet in scope.
    > >
    > > --
    > > I hope this helps,
    > > Steve C. Orr, MCSD
    > > [url]http://Steve.Orr.net[/url]
    > >
    > >
    > >
    > > "Kian Goh" <kiangoh2700544@hotmail.com> wrote in message
    > > news:ePw%23YfiUDHA.3916@tk2msftngp13.phx.gbl...
    > > > Hi,
    > > >
    > > > Can somebody tell me why can't I use
    > > > string strPath = Server.MapPath(Request.ApplicationPath);
    > > > in
    > > > protected void Application_Start(...)
    > > >
    > > > It compiles ok but an unhandled exception occurred during the
    execution.
    > > > Exception Details: System.NullReferenceException: Object reference not
    > set
    > > > to an instance of an object.
    > > >
    > > > I have no problem using the above code in private void Page_Load(...)
    > > >
    > > > Thanks,
    > > > Kian
    > > >
    > > >
    > >
    > >
    >
    >

    Steve C. Orr, MCSD Guest

  6. #5

    Default Using server.mappath

    Hi

    On my local server the physical db path is
    "C:/Inetpub/wwwroot/StaffDiary/Cal.mdb". I am using the virtual path as
    conn.Open Server.MapPath("Cal.mdb") but I am getting a;

    Microsoft JET Database Engine (0x80004005)
    Unspecified error

    error. What am I doing wrong?

    Thanks

    Regards


    John Guest

  7. #6

    Default Re: Using server.mappath

    Able Consulting - ADO Connection String Samples
    [url]http://www.able-consulting.com/ADO_Conn.htm[/url]

    Best regards,
    J. Paul Schmidt, Freelance ASP Web Designer
    [url]http://www.Bullschmidt.com[/url]
    ASP Designer Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...


    *** Sent via Devdex [url]http://www.devdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Bullschmidt Guest

  8. #7

    Default Re: Using server.mappath

    [url]http://www.aspfaq.com/2126[/url]

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)




    "John" <john@nospam.infovis.co.uk> wrote in message
    news:Oa6s$r9VEHA.3472@TK2MSFTNGP09.phx.gbl...
    > Hi
    >
    > On my local server the physical db path is
    > "C:/Inetpub/wwwroot/StaffDiary/Cal.mdb". I am using the virtual path as
    > conn.Open Server.MapPath("Cal.mdb") but I am getting a;
    >
    > Microsoft JET Database Engine (0x80004005)
    > Unspecified error
    >
    > error. What am I doing wrong?
    >
    > Thanks
    >
    > Regards
    >
    >

    Aaron [SQL Server MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139