We often use session objects to store user-specific information. Sometimes we get a requirement to get all the values in the Session object. For example, in Page1.aspx I am storing one value to the session and in Page2.aspx it is storing another value (a different key) in the session. In Page3.aspx I need to print all values.
Let's proceed to see how to get all the values in the session.
Page1.aspx.cs
In this page we are storing one value (Id) to the session.
- Session["Id"] = "1";
In this page we are storing another value (Name) to the session.
- Session["Name"] = "Manas Mohapatra";
Now we will retrieve all session values that are stored in the preceding pages. It can be done using the following options.
Option 1
In the following code we are looping over Session.Contents to get all the keys in the session. Later, using the key, we are getting the value from the session.
- protected void Page_Load(object sender, EventArgs e)
- {
- foreach(string key in Session.Contents)
- {
- string value = "Key: " + key +", Value: " + Session[key].ToString();
- Response.Write(value);
- }
- }
In the following code we are counting the number of keys present in the current session object. And using the session keys we are getting the values.
- protected void Page_Load(object sender, EventArgs e)
- {
- for (int i = 0; i < Session.Count; i++)
- {
- string value = "Key: " + Session.Keys[i] + ", Value: " + Session[Session.Keys[i]].ToString();
- Response.Write(value);
- }
- }
In the following code we are directly looping over the session that returns the key. Using that key we are getting the session values.
- protected void Page_Load(object sender, EventArgs e)
- {
- foreach (string s in Session)
- {
- string value = "Key: " + s + ", Value: " + Session[s].ToString();
- Response.Write(value);
- }
- }
See Figure 1 that shows the session key name and session value. We are looping over Session.Contents that holds all session keys. And using the key we are getting the session values.

Figure 1: Debugging to get all the values in the session
I hope this helps you how to get all the values in the session.


Gopalash KayalPosted Dec 27, 2023, 11:02 AM EST
Create "admin login & user login" in same page
Ramzanali MominPosted Aug 20, 2018, 11:30 PM EDT
Good jobss
Ramzanali MominPosted Aug 20, 2018, 11:29 PM EDT
Nice share
Pradeep SahooPosted May 28, 2016, 4:59 AM EDT
good share..
Sandeep KumarPosted Aug 19, 2015, 3:05 AM EDT
Nice article :)
Sibeesh VenuTOP 100Posted Aug 19, 2015, 2:54 AM EDT
Nice Share
Yashwanth MuthineniPosted Aug 19, 2015, 2:07 AM EDT
Nice Share
Piyush DixitPosted Aug 19, 2015, 12:21 AM EDT
nice
Anand MorePosted Aug 18, 2015, 10:13 PM EDT
thanks for share.
Sumit JoshiPosted Aug 18, 2015, 9:32 PM EDT
Nice Share.
Vaikesh K PPosted Aug 18, 2015, 9:21 PM EDT
Good Share
Mohammed IbrahimPosted Aug 18, 2015, 9:16 PM EDT
nice
Karthikeyan KPosted Aug 18, 2015, 7:12 PM EDT
Good one
Santhakumar MunuswamyPosted Aug 18, 2015, 10:29 AM EDT
Good one
Chervine BhiwooPosted Aug 18, 2015, 7:22 AM EDT
Nice
Vipul MalhotraPosted Aug 18, 2015, 4:53 AM EDT
nice
Jacob RobinsonPosted Aug 18, 2015, 4:27 AM EDT
Good Share..
Sibeesh VenuTOP 100Posted Aug 18, 2015, 4:15 AM EDT
Nice Share
Rahul PrajapatPosted Aug 18, 2015, 3:29 AM EDT
Nice article sir
Gopi ChandPosted Aug 18, 2015, 1:29 AM EDT
Good work dude
Rajeesh MenothPosted Aug 18, 2015, 12:27 AM EDT
Nice share
RakeshPosted Aug 17, 2015, 11:16 PM EDT
Nice one
Vaikesh K PPosted Aug 17, 2015, 11:04 PM EDT
Good one
Gowtham KTOP 100Posted Aug 17, 2015, 11:02 PM EDT
Good one
Karthikeyan KPosted Aug 17, 2015, 10:59 PM EDT
Good one sir
Upendra Pratap ShahiPosted Aug 17, 2015, 10:53 PM EDT
Nice article Manas Mohapatra
Nilesh JadavPosted Aug 17, 2015, 10:37 PM EDT
nice article sir