Wednesday 6 November 2013

How To Show All session Key and Value In ASP.NET

Leave a Comment
Session is a  powerful feature in web application to store information in server memory. by doing this, any data will be save temporary in server memory and can be access in the application.

This is a advantage and disadvantage of Session :

Advantages:

  • It helps maintain user state and data all over the application.
  • It is easy to implement and we can store any kind of object.
  • Stores client data separately.
  • Session is secure and transparent from the user.

Disadvantages:

  • Performance overhead in case of large volumes of data/user, because session data is stored in server memory.
  • Overhead involved in serializing and de-serializing session data, because in the case of StateServer and SQLServer session modes, we need to serialize the objects before storing them.
 More about session can read here

In this example, i will show how to create simple page to show all session key and value of session key. :

Requirement

  • aspx page and code behind

The Aspx Page

     <h1>
        Print All Session Key And Value</h1>
    <br />
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel> 


The Code Behind

protected void Page_Load(object sender, EventArgs e)
        {
            String table = "";
            table += "<table style=\"width:450px; border:1px solid #ccc;\"><tr>";
            table += "<th>Session Key</th>";            
            table += "<th>Session Value</th>";
            table += "</tr>";
   
            for (int i = 0; i < Session.Keys.Count; i++)
            {
                table += "<tr>";
                table += "<td>" + Session.Keys[i] + "</td>";                
                table += "<td>" + Session[i].ToString() + "</td>";
                table += "</tr>";              
            }

            table += "</table>";

            Panel1.Controls.Add(new LiteralControl(table));
        }

Sample Output











By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.