user1292656
user1292656

Reputation: 2560

Cast Session to object

I am assigning into a session a RadTreeNode object (i checked from debugger that the session is not null). And then i am trying to cast it back to RadTreeNode object with the following code.

 RadTreeNode node = new RadTreeNode();
      node =  (RadTreeNode)Session["nextNode"] as RadTreeNode;

But the node variable still null. Any suggestions ?

Upvotes: 2

Views: 3594

Answers (1)

kingdango
kingdango

Reputation: 3999

There is a good chance that Session["nextNode"] is null, as eluded to by @Richard in his comment.

The "as" cast as RadTreeNode will yield a null value if the source object does not convert to the target type. The prefix cast (RadTreeNode) targets a reference type and will not throw an exception when the as RadTreeNode cast fails.

You're casting a null value to RadTreeNode and you're getting what you're supposed to... a null reference of RadTreeNode.

Session is generally bad and should be avoided whenever at all possible. My guess is you're expecting Session to behave one way and its not.

For example, is RadTreeNode serializable? It's been a while so I don't remember how Session handles serializing/deserializing objects but my guess is it will eat any exceptions related to serialization. Admittedly a guess.

Upvotes: 1

Related Questions