using Server.MapPath

  • Thread starter Thread starter Kian Goh
  • Start date Start date
K

Kian Goh

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
 
There is no Request yet when your application is starting up.
The Request object is not yet in scope.
 
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());
====================================
 
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:
http://msdn.microsoft.com/library/d...onfigurationsettingsclassappsettingstopic.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_Start procedure.
Here's more info:
http://msdn.microsoft.com/library/d...stemwebhttpserverutilityclassmappathtopic.asp

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


Kian Goh said:
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 said:
There is no Request yet when your application is starting up.
The Request object is not yet in scope.
 
Back
Top