tags:

views:

48

answers:

2

Hi

I'm using jQuery to get a few values and them i send to an Webservice. On that WebService i'm checking if a session is null but i'm receiving Null Reference to that session and it gets error (i see it on javascript console, on debugging i just see the error on output "A first chance exception of type 'System.NullReferenceException' occurred in PromotorDeSaude.DLL".

I'm checking if session is null like this:

if (HttpContext.Current.Session["encomenda"] != null)

Am i doing something wrong?

+1  A: 

That suggests that HttpContext.Current or HttpContext.Current.Session may be null - which would happen if you're running on a different thread, for example.

I suggest you log every bit of the expression to find out which bit is null, and work from there.

Jon Skeet
I've just tried to create a new session trough "HttpContext.Current.Session["encomenda"]="a";" but i get null reference too.Is this the proper way to create an session, or it should be created in global.asax when the app begins for example?
Guilherme Cardoso
@Guilherme, did you try Jon's suggestion? He was indicating that *`HttpContext.Current`* itself might be null. Did you check for that? Also, you're not "creating a session" with that line. You're assigning the value `"a"` to the session under the name "encomenda".
Kirk Woll
It was my mistake, because i wasn't enabling sessions in WebMethod.
Guilherme Cardoso
+1  A: 

To access Session in a web service method, add the attribute

[WebMethod(EnableSession = true)]

to the method. See http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx for more information.

Andreas Paulsson
Thank you so much Andreas. In fact i don't know almost anything about WebMethods, i've started used them 2 days ago because JSON.I guess it's better to read a little more about WebMethods.Thanks again ;)
Guilherme Cardoso
You are welcome, I have done exactly the same mistake myself :-). If my answer fix your problem, please mark as answer.
Andreas Paulsson
I know, i'm waiting for the 3 minutes to accepet it (stackoverlow rule i guess)
Guilherme Cardoso