473,498 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 17 '05 #1
3 7248
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" <ki************@hotmail.com> wrote in message
news:eP****************@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

Nov 17 '05 #2
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" <St***@Orr.net> wrote in message
news:e2**************@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
http://Steve.Orr.net

"Kian Goh" <ki************@hotmail.com> wrote in message
news:eP****************@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


Nov 17 '05 #3
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/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_Start procedure.
Here's more info:
http://msdn.microsoft.com/library/de...ppathtopic.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Kian Goh" <ki************@hotmail.com> wrote in message
news:OL*************@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" <St***@Orr.net> wrote in message
news:e2**************@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
http://Steve.Orr.net

"Kian Goh" <ki************@hotmail.com> wrote in message
news:eP****************@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



Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
14291
by: John Rebbeck | last post by:
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 try to use Server.MapPath in that site it returns a...
0
3377
by: canasdaq | last post by:
Can anyone please help me. I was looking at the article "http://www.eggheadcafe.com/articles/20030603.asp". I am new to .net and know nothing in c#. I want to write a menu in asp.net. Can anyone...
1
1330
by: Stephen | last post by:
I have the following for a datagrid that inserts a row into an xml file. It works great, but inserts the data to a new top-level node. I don't know how to specifiy an existing node for the...
2
23869
by: Scott | last post by:
Hi there, I am writing a webservice and am wanting to use Server.MapPath to get the path to some files on the server that is hosting the webservice. I am using VB.NET atm, but am learning C#, so...
4
7155
by: vunet.us | last post by:
How to use server.mappath() parent folder correctly: Server.MapPath("../test.asp") Thank you for the hint.
3
15044
by: rn5a | last post by:
Server.MapPath returns the physical file path that corresponds to the specified virtual path whereas Request.MapPath maps the specified virtual path to a physical path. Assuming that a file named...
6
7924
by: withers | last post by:
If I run Server.MapPath("/") on my operational Internet server I get, correctly, the physical path to the directory my page is in (d:\.... \...\htdocs). If I run Server.MapPath("/") on my...
7
3197
by: jain2005k | last post by:
How can I rename a .jpg file in server using asp( not asp.net) code I use the FileSystemObject to do that like Fso = Server.CreateObject("Scripting.FileSystemObject");...
3
4111
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have one line code in my program, occasionaly I will receive the following error: "The Path parameter for the MapPath method did not correspond to a known path" ...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.