I am working on a site at the moment that requires me to store a lot of user entered values from form elements from one page to the next before a user can complete an order. This is unusual as normally the user just enters all the required values on one screen but for this particular project the design required the project to be setup to work like this.
ViewStates or hidden fields are a nice way to store form values on post back but it's very hard to carry those values across to other pages. The Session object was my only choice.
I currently use the asp.net session state service for all my projects. This is a little slower (around 10%-15%) than using the default session setup of in-proc but it has the advantage that the sessions themselves are a lot more stable and tend to last the right amount of time I set them to before they expire.
The main issue I had with the design was that it is a high volume site and that the number one requirement was that this new site was quick to use. I had around 15 values that needed to be carried through the site after page one and around 5 more items to be gathered from pages 2 and 3 before sending the values off to a third party through XML. A database table would have worked except that it would have been slower than using a session and I had some values that were in dynamic Lists which would made it trickier to save into a table.
I did some research and found that List objects are great but slow down if you store a lot of information in them so I decided to use a Dictionary object to hold the user values. I would then store this object into a Session to hold my values from page to page.
It's quiet easy to setup a Dictionary object to hold these user values. First thing you need to do is setup the object. Once you've done that you can add new items to it very easily. Once you have stored all your items into your Dictionary object, simply store it in a session.
using System.Collections.Generic;
//declare the dictionary object
Dictionary<string, string> UserPreferences = new Dictionary<string, string>();
//add some items to it
UserPreferences.Add("CustomerName", CustomerName.Text);
UserPreferences.Add("CustomerEmail", CustomerEmail.Text);
//save this object in a session
Session["UserPreferences"] = UserPreferences;
To get your values out of your session all you have to do is:
using System.Collections.Generic;
//declare your dictionary object and populate it from the session
Dictionary<string, string> UserPreferences = (Dictionary<string, string>)Session["UserPreferences"];
//pull out the values in the Dictionary by the key reference you setup
CustomerName.Text= UserPreferences["CustomerName"];
CustomerEmail.Text = UserPreferences["CustomerEmail"];
Although this isn't ideal, because of the current site setup I wasn't left with much of a choice but to go down this route. The downside to storing my values in a Dictionary object in a session using the State service is that every time I pull the item out of the session or store it to the session there is an overhead as .net serializes my data. It's a small price to pay though. On the plus side I have a setup that allows me to store a lot of variables but just use one session value to hold them.
This particular project is on a brand new Win Server 2008 with buckets of memory and so far, thankfully, the site has worked and scaled up quite well for us.
Member discussion