session returns null object in asp.net

15,904

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.

Share:
15,904
Shaggy
Author by

Shaggy

Updated on June 04, 2022

Comments

  • Shaggy
    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
    Rajeev Kumar almost 10 years
    You should put it as comment. This is not an answer
  • Shaggy
    Shaggy almost 10 years
    @Shark i did that. value goes in session varable
  • zblago
    zblago almost 10 years
    Is 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.