Session State Management
page 5 of 9
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 72359/ 119

Storing, retrieving and deleting objects in the Session

In order to add objects from the Session use the following method:

Listing 5

HttpSessionState.Session.Add(name,value);

This adds an object to the Session state, the name and the value of the item to be added being passed to this method as parameters.  Further, the HttpSessionState class is present in the System.Web namespace in the System.Web.dll file.

Alternatively, we can add objects to the Session.

Listing 6

HttpSessionState.Session[“UserName”] = “Joydip”;

This automatically creates a new item called “UserName” in the Session State Collection (if none exists) and sets its value to “Joydip.”

The following method is used to retrieve a previously stored object from the Session state.

Listing 7

string userName = HttpSessionState.Session["UserName"].ToString();

Note that the ToString() method is called because data is stored in the Session state as objects. Therefore, the conversion or casting to the appropriate type is necessary in the above example to retrieve the current User’s name as a string.

The following method is used to remove an object from the Session state.

HttpSessionState.Remove(objectName);

This method deletes the object specified as parameter from the Session State Collection, provided the same exists.  In this case, if the session-state collection does not contain the element specified, the collection remains unchanged, but no exception is thrown.

To clear the Session use either of the following:

Session.Abandon();

or

Session.Clear();

The difference between the Session.Abandon() and the Session.Clear() method is that if you call Session.Abandon(), Session_End will be fired (for InProc mode) and in the next request, Session_Start will be fired.  Session.Clear( ) just clears the session data without killing it.


View Entire Article

User Comments

Title: Really Nice   
Name: PRABHURAJAN_G
Date: 2006-12-22 9:36:38 AM
Comment:
Its really excellent, and i am so cleared with the ideas of session expiration
Title: Firefox and IE process session differently   
Name: Michael
Date: 2006-12-20 11:36:26 AM
Comment:
Hi,

I found firefox keeps same value of "ASP.NET_SessionId" session on every new opened browser window. But IE generated a new "ASP.NET_SessionId". So you could not share sesssion data between new opened browser window in IE.

Michael
Title: Session State questions   
Name: Michael
Date: 2006-12-18 4:28:19 PM
Comment:
Hi Joydip,

I could access to session using both "Response.Redirect(url,false);" and "Response.Redirect(url)". I really could not tell what's the differences between them.

Recently one my cowork set "sessionState mode='StateServer'" instead of "mode='InProc'", because he said the session were lost sometimes. Did you have this kind of problem? If you had it, please tell me how you fixed it.

By the way, if I have big data block, like a big hashtable, Should I save it to session or viewstate? Why?

Thanks.
Michael
Title: Great   
Name: Nadeem Abbasi
Date: 2006-12-08 7:45:55 AM
Comment:
It is very Comprehensive article about Session State Management and also great contribution from Joydip Kanjilal for developers.

--MNA_4U@HOTMAIL.COM
Title: Thanx   
Name: Sudeep
Date: 2006-12-07 3:31:09 AM
Comment:
Very much informative.
Title: state management   
Name: Nitu Singh
Date: 2006-12-06 3:10:28 AM
Comment:
It,s really too good.
Title: Very Nice   
Name: Rajesh
Date: 2006-11-29 8:40:13 PM
Comment:
Very nice article. So many gud points are covered about sessions state storage.

Thanks for the Author.
Title: Abt Session manage ment   
Name: Subhashree
Date: 2006-11-29 5:53:53 AM
Comment:
Excellant Article
Title: I like it too much   
Name: Nguyen Letan
Date: 2006-11-28 9:22:12 PM
Comment:
It's helpful for me. Thanks!
Title: Reema   
Name: Agarwal
Date: 2006-11-26 12:26:24 PM
Comment:
Simply Outstanding
Title: GOOD   
Name: KIRAN
Date: 2006-11-25 5:38:58 AM
Comment:
ITS VERY GOOD
Title: Nice   
Name: Ramana
Date: 2006-11-24 4:37:50 AM
Comment:
Way of explaination is good.
Title: IAtanasov   
Name: Ivan Atanasov
Date: 2006-11-23 9:42:35 AM
Comment:
This is fantastic advice for understand Session State!
Title: Excellant solution to understand session state   
Name: Doss
Date: 2006-11-18 6:03:51 AM
Comment:
It gives easy way to understand session state.Like this i want defn for state mangement .
Title: Session State Management   
Name: A. Ravi Srinivas
Date: 2006-11-18 12:22:56 AM
Comment:
This article seems to be excellent.
I got benefited from Joydip Kanjilal old article on collections as well.
Hope we will get many more from you Joydip Kanjilal
Thanks
Ravi

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 11:33:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search