I have an object MyObject and I'm storing that in the session.
I write this:
MyObject TheObject = HttpContext.Current.Session["TheObjInSession"] as MyObject;
I'm getting error Object reference not set to an instance of an object
Why? I thought that if you use as
to cast and the cast produces an error then it just returns null and execution continues. How should I rewrite this line?
Thanks for your suggestions.
PS: The error is produced because I removed the enable session line [WebMethod(EnableSession = true)]
I'll put the line back in but I want to make sure there are no exceptions in case anything goes wrong.