4

Actually I am trying to send data from .aspx to .ashx file. I am using Session in .aspx and trying to get value of session in .ashx but it is showing exception:: Object reference not set to an instance of an object.

Here is my code :-

.aspx code

[WebMethod(enableSession:true)]
public static string SetId(string Id)
{
    HttpContext.Current.Session["MId"] = Id.ToString(); Console.Write(" ");
    string session = HttpContext.Current.Session["MId"].ToString(); // Here I am getting value

    //string session = HttpContext.Current.Session["MId"].ToString();
    //BatchReadEmails.EmailProperties session = new BatchReadEmails.EmailProperties();
    //session.MailId = Id.ToString();

    return "Ok";
}

I am getting value in string session.
.ashx code:-

 public class ChangeLogHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
            {

                public void ProcessRequest(HttpContext context)
            {
                HttpRequest request = context.Request;
                HttpResponse response = context.Response;
                  string session = "";

                if (context.Session["MId"] != null)
                    session = context.Session["MId"].ToString();
                else
                    session = "Hello India";


            }
            }

Here it is showing session = "Hello India"

My Question:-

  1. Is there any way to send data from .aspx to .ashx file??

  2. I checked so many links all are using if for null but I already check in .aspx file it is showing value there but showing null in .ashx file Why?? (For exceptional cases we can use/ write if condition but I already checked string session has value.

Am I missing something?? Thanks

These are the links I already used:-

How to access Session in .ashx file?
Accessing context session variables in c#

Community
  • 1
  • 1
Shyam Dixit
  • 2,325
  • 3
  • 19
  • 27
  • I notice you're using a webmethod in the page to set the session value. Does it work if you set the session value to something in the main Page_Load? – Jon Egerton Jan 03 '14 at 11:36
  • Yes, It is coming,If I define in Page_Load – Shyam Dixit Jan 03 '14 at 11:56
  • Are you sure the webmethod is executed before the call to the ashx? – Jon Egerton Jan 03 '14 at 12:15
  • Yes it is executed. And am using 'session' variable to check whether it is containing something or not. – Shyam Dixit Jan 03 '14 at 12:16
  • Can you update the ashx code - the code shown doesn't compile (as the code is just in the class body). See my comment about SSCCE - its important if you want constructive help and people not getting frustrated. – Jon Egerton Jan 03 '14 at 12:30

3 Answers3

3

In the aspx you're adding Session["MId"]. In the ashx you're reading Session["MailId"].

Make the keys you're using the same. (ie either MId or MailId, but not both).

Would suggest you define a constant to define this value since it's shared, then you can avoid such problems.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • M sorry but both are same acutally I tried so many ways so at last it as MailId. But it is same there – Shyam Dixit Jan 03 '14 at 11:14
  • Well then make sure the code you show as having the problem is the code you're actually using that has the problem, not something else that has other issues. See [SSCCE](http://sscce.org/) for what is best to post to these sites. – Jon Egerton Jan 03 '14 at 11:35
2

Now it's working.I did these changes:-

Instead of

[WebMethod(enableSession:true)] 

I put

[WebMethod(EnableSession = true)]

But Both are the correct way.

And I include async: false in my ajax call. (I think before setting the session it was trying to read it)

Shyam Dixit
  • 2,325
  • 3
  • 19
  • 27
1

This code worked for me well

protected void Button1_Click(object sender, EventArgs e)
    {
        Session["MailId"] = "somemail@address.com";
        Response.Redirect("Handler.ashx");
    }

/// Code in ashx

public class Handler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest (HttpContext context)
    {

        string sessionValue = context.Session["MailId"].ToString();
        context.Response.Write(sessionValue);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

I like to know whether u accessing handler from an ajax call or not

Binson Eldhose
  • 749
  • 1
  • 6
  • 14