Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET C# Session Variables Being Lost

I have a solution which includes 2 projects and 2 class files that are called by reference. In one of my projects, I have some code in Global.asax in the Session_Start block that loads a few variables from a database and sets them into session variables. If I put a break point in the Global.asax I can verify that the variables are in fact being set correctly.

When I reference the session variables in classes in any of my code-behind modules or a class in my project, they are there. But if I reference them in one of the classes that is called by reference (a shared class, essentially), the Session variables are all null.

I am using HttpContext.Current.Session["varName"] to access the variables in a class, as is standard.

Is there something else I need to consider to have access to these session variables? Could it perhaps be a namespace issue?

like image 649
Doug Wolfgram Avatar asked Sep 29 '11 23:09

Doug Wolfgram


People also ask

What is ASP.NET C?

ASP.NET is an open source web framework, created by Microsoft, for building modern web apps and services with . NET. ASP.NET is cross platform and runs on Linux, Windows, macOS, and Docker.

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

Is ASP.NET like C#?

ASP.NET is a web application development framework used to develop web applications using different back-end programming languages like C# where C# is used as an object-oriented programming language to develop web applications along with ASP.NET.

Can I learn ASP.NET without C#?

NET Common Language Runtime, allowing web pages to be coded in VB.NET, C#, J#, Delphi.NET, Chrome etc. This means you don't have to learn C# to use it and, even if you don't know any of those languages, you may find it easier to start with a more human readable one such as VB.NET.


2 Answers

I had the same issue before, I keep on losing the my session variables (although not in the same context as yours). I found this articles helpful for my issue: ASP.NET Case Study: Lost session variables and appdomain recycles and PRB: Session Data Is Lost When You Use ASP.NET InProc Session State Mode. Hope it might help you too. Cheers!

like image 66
Annie Lagang Avatar answered Oct 05 '22 23:10

Annie Lagang


Are you calling Session.Abandon() anywhere in the code? I was doing this at the start of my Web App to ensure I was starting with a "fresh" session. Turns out that any Session variables stored even after the "Abandon" would be dropped (even if the SessionID was forced to remain the same via other means, such as using Server.Transfer(Url, true) rather than Response.Redirect), upon postback.

i.e. I could trace into my app, watch all the session variables be correctly set, and then the moment any event handler (anything with AutoPostBack="True", like a checkbox or button on an UpdatePanel) was called, BAM, I had the same SessionID, but zero session variables.

Removing the pre-emptive call to Session.Abandon() solved my problem straightaway.

Jeff

like image 32
Jeff Woods Avatar answered Oct 06 '22 01:10

Jeff Woods