Loading session values - best practice?
I've got a complex status page in asp.net. I'm stashing a bunch of values in the session to maintain the user-defined state of the page.
I'm a Java person, but I usually use your first method. I wrote a little method to do it for me, and it works fine.
!El gato es muy gordo!
I usually have all the variables I need in a class, then I just save and load the object to Session. Their is probably something horribly inefficient with this method, but so far it doesn't seem to be a problem.
Your first method, as written, will not work. It attempts to cast to a string which will throw an exception if it's anything but a string, then attempts to cast as an int which will throw an exception if it's anything but an int. Casting doesn't perform conversion between types.
Create a class with all the variables (strong typed). Serialize to XML and put the XML into the session object.
The XML class option can also be used to store the state in an XML data island on the page or in the viewstate.
(a) Do XML data islands work in Netscape?
I don't know?
> (a) Do XML data islands work in Netscape?
Philo - If you have MS Application Center test, it would be interesting to know how much of a difference between the two methods posted and a third of doing nothing and just reloading the values.
So, how do you transform a class to XML, quickly, using C#?
Forget the XML -- why bother? Session can store any objects. I note you were storing the key as a string - again why? ANY object/value type can be stored in Session. If you're in a webfarm and using StateServer session or SQL-backed session then the classes you store in Session need to be marked as [Serializable].
I prefer to create a class to handle session & declare the session values as public properties. That way, the casting is all done within the class & you can use Intellisense to make sure the values you're getting/setting are typed correctly.
Fog Creek Home