Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a button that when I click this method is executed (after JS - ajax):

C#
[System.Web.Services.WebMethod]
   public static string carregarNotas()
   {
       try
       {
           string id;
           id = Session["weeksLength"].ToString();
           string user = Session["username"].ToString();

//.........


The problem is, I get "An object reference is required". If I place that id = ... on page_load I dont get this problem.

How can I solve this problem? I execute an SQL statement in this method and I need that session variable.

thanks in advance.
Posted

It usually helps when you read the documentation.

http://msdn.microsoft.com/en-us/library/ykshe9s9.aspx[^]

Parameters

enableSession
Type: System.Boolean
Initializes whether session state is enabled for the XML Web service method.

Without this the Session object is not valid.
 
Share this answer
 
Comments
Maxdd 7 22-Dec-10 13:26pm    
That dont work either inside a static method.
As I said, my own answer and the explanation:

The problem is that the Session object is a non static member of the Page/Control class that I was using.

So I have to use:

C#
static void testStaticFunc()
{
  HttpContext.Current.Session["test"] = "testString";
}



And it works.

Thanks anyway.
 
Share this answer
 
Comments
[no name] 22-Dec-10 15:42pm    
You didn't specify you were calling this from a Page. Could have been a webservice method, in which case you would need the enableSession variable.
Maxdd 7 23-Dec-10 0:29am    
Yes you right, thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900