using Server.MapPath

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kian Goh

    #1

    using Server.MapPath

    Hi,

    Can somebody tell me why can't I use
    string strPath = Server.MapPath( Request.Applica tionPath);
    in
    protected void Application_Sta rt(...)

    It compiles ok but an unhandled exception occurred during the execution.
    Exception Details: System.NullRefe renceException: 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

    #2
    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




    "Kian Goh" <kiangoh2700544 @hotmail.com> wrote in message
    news:ePw%23YfiU DHA.3916@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Hi,
    >
    > Can somebody tell me why can't I use
    > string strPath = Server.MapPath( Request.Applica tionPath);
    > in
    > protected void Application_Sta rt(...)
    >
    > It compiles ok but an unhandled exception occurred during the execution.
    > Exception Details: System.NullRefe renceException: 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
    >
    >[/color]


    Comment

    • Kian Goh

      #3
      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_Sta rt(...)
      =============== =============== ======
      StreamReader sr = File.OpenText(" d:\\kk\\omac\\o macCounter.txt" );
      Application["Counter"] =
      System.Convert. ToInt16(sr.Read ToEnd().ToStrin g());
      =============== =============== ======



      "Steve C. Orr, MCSD" <Steve@Orr.ne t> wrote in message
      news:e2dVAoiUDH A.2316@TK2MSFTN GP09.phx.gbl...[color=blue]
      > 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
      > http://Steve.Orr.net
      >
      >
      >
      > "Kian Goh" <kiangoh2700544 @hotmail.com> wrote in message
      > news:ePw%23YfiU DHA.3916@tk2msf tngp13.phx.gbl. ..[color=green]
      > > Hi,
      > >
      > > Can somebody tell me why can't I use
      > > string strPath = Server.MapPath( Request.Applica tionPath);
      > > in
      > > protected void Application_Sta rt(...)
      > >
      > > It compiles ok but an unhandled exception occurred during the execution.
      > > Exception Details: System.NullRefe renceException: Object reference not[/color][/color]
      set[color=blue][color=green]
      > > to an instance of an object.
      > >
      > > I have no problem using the above code in private void Page_Load(...)
      > >
      > > Thanks,
      > > Kian
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Steve C. Orr, MCSD

        #4
        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 = ConfigurationSe ttings.AppSetti ngs("AppPath")
        Here's more info:
        http://msdn.microsoft.com/library/de...tingstopic.asp

        But a better solution might be to user the Server.Mappath method instead. I
        believe the Server object is in scope in the Application_Sta rt procedure.
        Here's more info:
        http://msdn.microsoft.com/library/de...ppathtopic.asp

        --
        I hope this helps,
        Steve C. Orr, MCSD



        "Kian Goh" <kiangoh2700544 @hotmail.com> wrote in message
        news:OLlwcziUDH A.360@TK2MSFTNG P11.phx.gbl...[color=blue]
        > 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[/color]
        from[color=blue]
        > 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[/color]
        way[color=blue]
        > 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_Sta rt(...)
        > =============== =============== ======
        > StreamReader sr = File.OpenText(" d:\\kk\\omac\\o macCounter.txt" );
        > Application["Counter"] =
        > System.Convert. ToInt16(sr.Read ToEnd().ToStrin g());
        > =============== =============== ======
        >
        >
        >
        > "Steve C. Orr, MCSD" <Steve@Orr.ne t> wrote in message
        > news:e2dVAoiUDH A.2316@TK2MSFTN GP09.phx.gbl...[color=green]
        > > 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
        > > http://Steve.Orr.net
        > >
        > >
        > >
        > > "Kian Goh" <kiangoh2700544 @hotmail.com> wrote in message
        > > news:ePw%23YfiU DHA.3916@tk2msf tngp13.phx.gbl. ..[color=darkred]
        > > > Hi,
        > > >
        > > > Can somebody tell me why can't I use
        > > > string strPath = Server.MapPath( Request.Applica tionPath);
        > > > in
        > > > protected void Application_Sta rt(...)
        > > >
        > > > It compiles ok but an unhandled exception occurred during the[/color][/color][/color]
        execution.[color=blue][color=green][color=darkred]
        > > > Exception Details: System.NullRefe renceException: Object reference not[/color][/color]
        > set[color=green][color=darkred]
        > > > to an instance of an object.
        > > >
        > > > I have no problem using the above code in private void Page_Load(...)
        > > >
        > > > Thanks,
        > > > Kian
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...