Storing a hash table in Session

  • Thread starter Thread starter rgparkins
  • Start date Start date
R

rgparkins

Hi
I am creating a sign-up process on a web site much like that of a
wizard form. I have browsed many sites to look for examples of how to
store the entry data, so that the user can go back and forward along
the sign -up process. I am currently trying to use a Session object to
store all these details and get them back out of session as each step
is selected.

My model was a HashTable of NameValueCollection, so I could go to the
hash for the particular step, and get the name key value pairs of each
data.

My problem is when I go to recollect the session object (Effectively a
hash table). The object has no data, ie Hash count items = 0. however
when I store this hash table to the Session object it has 5 or 6 items
..

There is something obviously happening when the object is being stored,
maybe a serialization issue.. I am new to this development so would
appreciate any pointers that people may have, or another technique I
can use? Perhaps NameValueCollection or HashTable cant be stored in
session? but why then are they serilaizable?

Many thanks

Richard
 
Richard,

If it was a serialization issue, then you would get an error, you
wouldn't just have the session drop like that.

Are you sure you have sessions enabled (through cookies, or cookieless)?
 
Hi Nicholas,

Many thanks for your reply..Below is my sessionState node from my
config file

Rich

<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data
source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
 
Richard,
Could you post a "Short but Complete" code example of how you are populating
your "Hashtable-like" object, storing in Session, then retrieving from
Session?

Peter
 
Hi Nicholas as an aside, I seem to have a valid hashtable stored in the
session under that variable as the speciric cast to my Wizard session
object is working with no exceptions thrown: below is my class just in
case I cam doing something completely incorrect (which is alway
possible :))
/// <summary>
/// Summary description for AddStockSession.
/// </summary>
public class WizardSession : Hashtable {
public WizardSession():base() {

}

/// <summary>
/// Add an object to the current context
/// </summary>
/// <param name="context">The context to add this key/pair to. Is the
hashcode of the </param>
/// <param name="key">The key to get this variable</param>
/// <param name="value">The value of the variable</param>
public void Add(string context, string name, string value) {
NameValueCollection collection ;

//retrieve the collection at this context
if (this[context] == null) {
//create it is it does no exist
collection = new NameValueCollection();
base.Add(context, new NameValueCollection());
}

//get the collection
collection = (NameValueCollection)this[context];

if (collection[name] != null) {
//if it exists remove
collection.Remove(name);
}

collection.Add(name, value);

//reassign (I think needed as references are used);
base[context] = collection;
}


/// <summary>
/// Returns the value of the variable on the page
/// </summary>
/// <param name="context">The context to get, is the hashcode</param>
/// <param name="key">The variable that is needed</param>
/// <returns>The variable as a string. String.Empty if fail</returns>
public string Get(string context, string key) {
if (this[context] != null) {
NameValueCollection collection =
(NameValueCollection)this[context];

if (collection[key] != null) {
return collection[key].ToString();
}
}

return String.Empty;
}
}
 
Richard,
Maybe its late and I'm not thinking, but I don't see anywhere in your code
where you are adding any name/value pairs to your NameValueCollection before
using it as the second parameter to the Hashtable's Add method.

What I'd do is simplify this:
1) use a regular Hashtable
2) have a small class with an Name and a Value field.
3) in your add method, create an instance of this class and populate the
fields,
4) Then add this to the Hashtable using context as the key, and your class
as the value.
Peter
 
Hi Nicholas,

Having spent a fantastic amount of time on this, it finally became a
gotcha... The session was being re-initilised on a ispostback call on a
page load!! I need to look into why when I click on my "Next" button is
defines ispostback as false, but hey thats another issue.

For now I would like to thankyou for being my "cardboard janitor", I
dont normally post on gotchas like this but was struggling today.. But
at least I know that storing these things in sessions is probebly the
right way to go.... in fact I am developing this class so that it loops
on all controls within a Panel within a user control to make it
automatic when you add the control to any page, (using wither the
hashcode for the control or the user defined ID)... not sure why .NET
1.1 doesn;t have this kind of facility anyway... Perhaps time for .NET
2.0 which I see has this as standard :)

Many thanks

Richard
 
Hi Nicholas as an aside, I seem to have a valid hashtable stored in the
session under that variable as the speciric cast to my Wizard session
object is working with no exceptions thrown: below is my class just in
case I cam doing something completely incorrect (which is alway
possible :))
/// <summary>
/// Summary description for AddStockSession.
/// </summary>
public class WizardSession : Hashtable {
public WizardSession():base() {

}

/// <summary>
/// Add an object to the current context
/// </summary>
/// <param name="context">The context to add this key/pair to. Is the
hashcode of the </param>
/// <param name="key">The key to get this variable</param>
/// <param name="value">The value of the variable</param>
public void Add(string context, string name, string value) {
NameValueCollection collection ;

//retrieve the collection at this context
if (this[context] == null) {
//create it is it does no exist
collection = new NameValueCollection();
base.Add(context, new NameValueCollection());
}

//get the collection
collection = (NameValueCollection)this[context];

if (collection[name] != null) {
//if it exists remove
collection.Remove(name);
}

collection.Add(name, value);

//reassign (I think needed as references are used);
base[context] = collection;
}


/// <summary>
/// Returns the value of the variable on the page
/// </summary>
/// <param name="context">The context to get, is the hashcode</param>
/// <param name="key">The variable that is needed</param>
/// <returns>The variable as a string. String.Empty if fail</returns>
public string Get(string context, string key) {
if (this[context] != null) {
NameValueCollection collection =
(NameValueCollection)this[context];

if (collection[key] != null) {
return collection[key].ToString();
}
}

return String.Empty;
}
}
 
HI Nicholas/Peter

Many thanks for your replies... I finally found out the issue, and
unfortunately it was a gotcha... My Page_Load even was re-initilaising
my Search, therefore no items:( Very embarrassed, I do not usually
post these things but have spent a LONG time on this today and wanted
to throw it out there in case there was some kind of serialization
issue, which as you may have guessed I was focusing on... I guess I
have learnt that this seems the correct way to do the storing of data
on a wizard process, so thankyou for that.

I am surprised that someone hasn;t developed a framework for this, I am
looking to develop this for all our client sites so making the class
generic, using control ids etc for referencing in the wizard object. (I
do this with PHP all the time and its very successful).

I guess .NET 2.0 is the way to go as I see this has this as standard, I
am also finding that paging data in .NET 1.1 with SQL Server 2000 is a
"grey" area as well, so maybe time to upgrade to Server 2005/.NET 2.0

Anyway many thanks for your replies, its nice to know that there is
someone to help and vica versa.

Richard
 
Back
Top