session returns null object in asp.net
If it is really the line of code you showed, that produces exception, it means your "Normal Business Logic" is running in a non-HttpRequest handling thread, where thing such as Context and Session are not available. Check that HttpContext.Current
and HttpContext.Current.Session
are not null.
Update:
After seeing your edit - to have access to Session, your handler must include IRequiresSessionState
in its class declaration. See: HttpContext.Current.Session is null in Ashx file.
Shaggy
Updated on June 04, 2022Comments
-
Shaggy almost 2 years
I am developing asp.net web application in which i am using
Session
to store user related data.Though i am storing data in session object when i am trying to retrieve it gives me error :Object reference not set to an instance of an object.
Setting session variable
somewhere in code behind of aspx page (myfile.aspx.cs)
HttpContext.Current.Session["ProjectID"] = Request.QueryString["Pid"].ToString();
Retrieving Session Variable
Normal Business Logic C# Class (Someclass.cs)
sProjectID = HttpContext.Current.Session["ProjectID"].ToString();
It gives me error on above line. It looks weird to me. can anyone explain this ?
Edit:
Call to class pass through generic handler (.ashx) page
-
Rajeev Kumar almost 10 yearsYou should put it as comment. This is not an answer
-
Shaggy almost 10 years@Shark i did that. value goes in session varable
-
zblago almost 10 yearsIs your business logic in separate assembly (project inside Visual Studio Solution). If it try to get a value from the session and pass like method parameter. Also, check answer from modiX.