1

I am setting a session value in the Handler page and want to use that in other page, when I access the session value in other page I get the error saying Object reference not set to an instance of an object.

string ad=Session["StackOverflow"].ToString();

How to access the session value in c# .aspx page

public class Upload : IHttpHandler, IRequiresSessionState{
    public void ProcessRequest (HttpContext context) {

    context.Session.Add("StackOverflow",filename);    
}}
Murthy
  • 1,502
  • 11
  • 31
  • 45

1 Answers1

2

What you have there should work except it is worth noting that you need to include an implementation of the IRequiresSessionState interface.

using System.Web.Sessionstate;

 public class Handler: IHttpHandler, IRequiresSessionState
 {

    public void ProcessRequest(HttpContext context)
    {
       context.Session["YourSessionVar"] = yourStringVar;
    }
 }

EDIT: OP edited question after post

In order to access the variable in your other page be sure to implement IRequiresSessionState on that page too. If you fail to do this you will not have access to the session variables.

EDIT: Futher info requested by OP

In order to access the session variable on your aspx page do the following:

using Sytem.Web.SessionState;

public class YourClass : IRequiresSessionState
{
    public string MyVar;

    protected void Page_Load(object senser, EventArgs e)
    {
        MyVar = Session["YourSessionVarName"].ToString();
    }
}

Now to add this into your onclick function in the html/aspx page you do this

<div onclick="yourJScriptFunction('<% Response.Write(MyVar) %>');">
CSharpened
  • 11,674
  • 14
  • 52
  • 86
  • I have made some changes to my question, can you look at it thanks – Murthy Feb 06 '12 at 10:29
  • The error you mention will come about if you have not set the session variable. Your session variable will not exist if it has not been assigned a value. I can only suggest that you double check that you are putting something into the session variable. Your code looks fine so must be a case of either adding a null value to the session var or not calling the code that creates and instantiates the variable. – CSharpened Feb 06 '12 at 10:30
  • I am creating the session named stack overflow in the handler, should I create the session variable elsewhere and just assign the value in the handler? – Murthy Feb 06 '12 at 10:34
  • No it is fine to declare it in the handler and then access it in another page provided you attempt to access it AFTER it has been declared and initialised. I just double checked it and it works fine for me. Have you stepped through your code to ensure that your names match and that there is definetly something being assigned to your variable? Also be sure that you have IRequiresSessionState in your page you want to access it from. – CSharpened Feb 06 '12 at 10:45
  • How to have IRequiresSessionState in my page where I am trying to access it. I want the value in a onClick method in my .aspx page – Murthy Feb 06 '12 at 11:04
  • You add a using directive at the top Using Sytem.Web.SessionState; and then implement the IRequiresSessionState interface like so: public class YourClass : IRequiresSessionState. Then you can access the session variable. If you then set a public variable in your code behind on your Page_Load() function you can then add it into your onclick method in your aspx page html code like this: onclick="yourFunction('<% Response.Write(YourPublicVariableName) %>');" – CSharpened Feb 06 '12 at 11:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7381/discussion-between-csharpened-and-murthy) – CSharpened Feb 06 '12 at 11:23