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 ?
There is a good chance that Session[“nextNode”] is null, as eluded to by @Richard in his comment.
The “as” cast
as RadTreeNodewill 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 theas RadTreeNodecast 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
RadTreeNodeserializable? 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.