473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disappearing Session Variables

8 New Member
I have been struggling with unexpected error messages on an ASP.NET system, using SQL and C#. The application draws organisation charts, based on data stored in the SQL database. Some of the chart editing processes place a very heavy load on the server as the effects of the edit ripple through the organisation structure, requiring potentially large numbers of rows in one of the tables to be updated. (I have done it this way to make the more commonly used reading process fast.)

The errors that I am getting all relate to apparently uninitialised session variables, namely: “System.NullRef erenceException : Object reference not set to an instance of an object”. After lots of checking, I have come to the conclusion that the server is somehow losing the session variables or that the absence of any SQL table locking during these updates might be involved.

When users first log in, they land on what I call the “User Home Page”, which sets defaults for the session variables. Unfortunately, if the session times out and the user later tries to continue working, the ASP.NET system logs him in again and takes him straight back to the page he was working on previously. This means that the session variables are not set because the user did not get there via the User Home Page. To solve this, I added the following code to the top of the User Home Page:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ((Session["TimeOut"] != null) && (Session["TimeOut"].ToString() != "False"))
  3. {
  4.      Time_Out_Label.Text = "Suitable error message";
  5. }
  6. Session["TimeOut"] = "False";
  7.  
  8.  
This prints an error message if the session variable “TimeOut” is initialised and set to anything other than false. (Note that the second part of the “if” statement is not evaluated if “TimeOut” is not initialised, which would otherwise result in the very error message I am trying to sort out.) At the top of all the other pages, I added the following code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (Session["TimeOut"] == null)
  3. {
  4.      Session["TimeOut"] = "True";
  5.      Response.Redirect("../Members/user_home.aspx");
  6. }
  7.  
  8.  
The idea is that if the session times out, but ASP.NET allows the user to log in again and carry on working without going via the User Home page, then “TimeOut” will be uninitialised (because the session has timed out); this will be caught by the “if” statement and the user will be redirected to the User Home Page with “TimeOut” set to true.

It seems to work, in that if I allow the session to time out and then attempt to continue, I get transferred to the User Home Page (with the intended error message) instead of having everything collapse in a big heap when the code tries to access an uninitialised session variable on the page that the user was attempting to view.

The problem is that I also occasionally get transferred to the User Home Page in this way during normal operation, i.e. within a few seconds of executing some other action, when clearly the session should not have timed out. I guess that in one sense this is good news, because it means that the original uninitialised-variable error messages were not due to errors in the code, but why are the session variables being forgotten by the system?

The site is hosted on a shared server at One and One. I’ve since been wondering what the server does if it runs out of storage space for session variables. Does it just kill off a few? Maybe at busy times, it doesn’t like my organisation chart editing process, which could involve several hundred separate database accesses for a single page update and therefore it simply kills the process (and the session variables). I have checked the entire project and there are no other references to "TimeOut" anywhere in the code.

Any ideas would be very much appreciated.

ChrisAtWokingha m
Aug 1 '08 #1
6 3779
shweta123
692 Recognized Expert Contributor
Hi,

I have following suggestion regarding the error you are getting in your code :

1> When the user will login through the Home page of the website his Login name should be set in a Session variable.
e.g. Session["Login"] = username

2> Now , you can use this Session variable in all your web pages in order to check if some user has logged in or not.
e.g. if(Session["Login"] == "")
{
Response.Redire ct("HomePage.as px");
}

3> Everytime when user Logs out OR session is time out , in that case, you
should make session variable as empty.
On Logout.aspx page you should write this code :
e.g. Session["Login"] = ""
Aug 1 '08 #2
ChrisAtWokingham
8 New Member
Hi,


Thank you for your reply. What you are suggesting is not materially different to what I am already doing. You are using the word "Login" as the session variable instead of "TimeOut" and you are putting the username or null in it whereas I am putting "true", false" or null in it. I am not trying to check whether someone has logged in or out; the ASP.NET system can tell me that. I'm trying to stop code running when the session variables have been erased through a time-out and renewed log in. That bit already works.

The problem is that the session variables are being cleared by the server when neither a log-out nor a time-out has occured. The Response.Redire ct route is sometimes being taken when the user causes a page re-submit even though the user has not logged out and has only been inactive for a few seconds, which is well within the typical 20 minute time out period. Under normal operation, when the user neither logs out nor is idle for more than a minute or so, the Response.Redire ct should never execute, but it is doing so.

regards,


ChrisAtWokingha m
Aug 1 '08 #3
Plater
7,872 Recognized Expert Expert
A number of things can lead to session loss.
The client broswer can reject the cookie for whatever reason (generally it's either a "I can use session cookies" or "no session cookies allowed" right from the start, and not mid-way through using a site)

Anytime you make changes to the code/upload/publish your website, all session data is dropped and reset.

I *think* IIS will prune Session data if the Session is using up a lot of space, i.e. more then it's allowed. If you suspect this, get in contact with your host and see if they have any log of IIS performing these actions.


Your global.asax file could be usefull here, as it contains the event handler for when a Session_End or Session_Start occurs. there is also the Application_Sta rt, Application_End and Application_Err or that could be usefull.
Aug 1 '08 #4
ChrisAtWokingham
8 New Member
Many thanks - you have given me a couple of pointers to investigate. The session variables are being lost occasionally in the middle of browsing, so it is not that the browser is rejecting cookies. I had already deduced that uploading revised code etc. would kill the session and I'm not doing anything like that, but I do have quite a few session variables. I'll see if I can reduce the number and if the problem persists, then contact the host as you suggest. Many thanks again.

regards,


ChrisAtWokingha m
Aug 5 '08 #5
shweta123
692 Recognized Expert Contributor
Hi,

If it is possible to replace session variables in your code you can try for other methods :
e.g.
1>You can pass the data from one page to another using querystring
2> Also you can use hidden fields for your requirement.
Aug 5 '08 #6
Frinavale
9,735 Recognized Expert Moderator Expert
Many thanks - you have given me a couple of pointers to investigate. The session variables are being lost occasionally in the middle of browsing, so it is not that the browser is rejecting cookies. I had already deduced that uploading revised code etc. would kill the session and I'm not doing anything like that, but I do have quite a few session variables. I'll see if I can reduce the number and if the problem persists, then contact the host as you suggest. Many thanks again.

regards,


ChrisAtWokingha m

My initial guess would be that you are running out of memory because your process is quite intensive. When this happens your worker process is recycled and your session is lost.

To get around this I would recommend changing your session from being InProc to SQLServer or something else instead. See this article on Sessions for more information.

-Frinny
Aug 5 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3331
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background information, I have a customized 500-100 page that sends the value of various session variables via email to my support site. The situation: On the home page of the site, the FIRST THING that is done is a Session
14
3234
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you go to is a login form, which will set several session variables with the name used to log in, appropriate security level and some other misc variables, and then will go to a main menu for each particular security level using Server.Transfer. ...
1
2100
by: Wiktor Zychla | last post by:
Hello there, I've just encountered a strange problem with Session. In one particular scenario it is cleared between pages but the scenario is so specific that I am really, really startled. I've tried to look for similar situations in the group archive and it seems that few people have observed similar behaviour. None of them, however, got a clear explanation that would correspond to my problem. In my web application I put some...
24
4096
by: Pink Pig | last post by:
I'm trying to track down an annoying problem that only occurs when I access my pages on a remote server using IE6. If I run instead run from localhost, IE6 works fine, and if I use another browser on the remote site, there are no problems. I'm not really looking for help debugging. I'd just like to be pointed in the right direction. Essentially, what happens is this. When I start up (i.e. load index.php on my site), I am logged out....
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.