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