Click here to Skip to main content
15,898,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have an aspx page with a treeview in it.
On clicking one treeview node I create a session, eg. session ["projectname"] = treeview1.selectednode.text;

but sometimes it happens that an error is thrown -- object reference not set to an instance of object for the above session--

I have ensured that the session always gets created before user going to another page, why this is happening?

I think is it related with some inproc, outproc session management, do I have to add below lines:
XML
<sessionstate mode="InProc" timeout="60"></sessionstate>

In my web.config? will this solve the problem?

Please guide me..

thankyou very much in advance.
Posted
Updated 21-Nov-10 23:10pm
v5
Comments
Dalek Dave 22-Nov-10 5:10am    
Edited for Readability and Syntax.

It simply means that the value of 'session["projectname"]' is getting cleared out. This would be the case when session is getting timedout.

If you make the change:
XML
<sessionstate mode="InProc" timeout="60"></sessionstate>

This would make sure that the session is kept intact for 60 minutes. Once it's over or IIS is reset/app-pool recycles then it will be lost.
 
Share this answer
 
Comments
Dalek Dave 22-Nov-10 5:10am    
Good Answer.
There are several reasons a worker process may get recycled thus dropping all currently active sessions. An application has to cope with sessions being dropped. Anything that needs to have a life time that exceeds a possible session should be stored in a DB. Sessions are meant to be used for storing temporary stuff for convenience only.

(Eventhough you can tune the values for the automatic recycling of the worker process, it can still happen that an iisreset is issued on a machine (after an 'wlbs suspend' perhaps) causing it to instantly loose all session state, unless your using an asp state session server hooked up to a MS SQL Server.)

Start Modification:
Just found this on I found this on http://www.xefteri.com/articles/show.cfm?id=14 :

<processmodel>
timeout="168:00:00"
...

The first involves the timeout attribute, which simply creates a new process after the amount of time specified as a value. For example, the above setting will automatically start a new process after 168 hours, or one week. The time clock starts right after the first request is made, because the process is actually created when the first request to the ASP.NET engine is made. This setting can be extremely useful in scenarios where there is a slow leak in memory and performance, and periodic IIS resets are needed.

1 ...
2 <processModel
3 requestLimit="10000"
4 ...

A second way is to use the requestLimit attribute and give it an Integer value. A value of 10,000 like above, will start a new process after 10,000 requests have been made. This can be useful if our web server's performance degrades after a set number of requests, instead of simply some amount of time past.

1 ...
2 <processModel
3 memoryLimit="50"
4 ...

A third way is to let your system watch how much memory the process is consuming. In the above example, the attribute memoryLimit is set to 50, which means that if the process uses more than 50% of total system memory then the process is killed , a new one is created and all existing requests are reassigned to the new one. This is extemely helpful is cases where a memory leak is present, no matter how slow the leak is.

1 ...
2 <processModel
3 responseDeadlockInterval="00:03:00"
4 ...

A fourth way of doing this is by using the responseDeadlockInterval attribute. The time setting above of 3 minutes will restart the process if two things happen: there are requests in the queue, but there have not been any responses for the last 3 minutes.

1 ...
2 <processModel
3 pingFrequency="00:00:30"
4 pingTimeout="00:00:05"
5 ...

The fifth and last way of recycling the process, is to use the pingFrequency and pingTimeout attributes in conjunction. The system pings the ASP.NET process at the pingFrequency interval, and restarts it if there is no response within the pingTimeout time interval.

End Modification


Cheers

Manfred
 
Share this answer
 
v3
Comments
Ankur\m/ 22-Nov-10 5:42am    
Your answer answers the question best, so I have given a 5. But I still have a doubt -
The OP said: "but 'sometimes' it happens that an error is thrown - object reference not set to an instance of object for the above session". Worker process recycling seems to be the most probable reason considering session timeout duration is set high enough. But since OP seems to have faced this issue many a times, I am curious if it is happening frequently on OP's server. If that's the case, there is something really wrong with the server. What do you think?
Manfred Rudolf Bihy 22-Nov-10 6:35am    
Found a site that lists 5 reasons (plus their corresponding parameters) that control worker process recycling.
As you said, which session mode you are using.If inProc then add the line
<sessionstate mode="InProc" timeout="60"></sessionstate>

in config file.

Also, before accessing any value from session, put a null check, because a session can be destroyed by many reasons. So to avoid YSOD, have a null check, show some error or redirect to some other page if required.
 
Share this answer
 
You need to check the session before assigning the value. because session may expire after 60 mins. so if u click after 60 mins u will get this error.

So check the session linke,
if(session["projectname"] != null)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900