๐ ASP.NET Session vs Classic ASP โ Store and Retrieve State with C#
๐งฒ Introduction โ Why Use Session in ASP.NET?
Web applications are stateless by natureโmeaning, they forget user data between page requests. Thatโs where Session comes in.
ASP.NET Session lets you store user-specific data on the server, like:
- Logged-in user info
- Shopping cart items
- Temporary form values
- User roles or preferences
๐ฏ In this guide, you’ll learn:
- What Session state is and how it works
- How to store and retrieve data using ASP.NET Session
- Difference between Classic ASP and ASP.NET Session
- Complete code examples with explanations and output
๐ ASP.NET Session Use โ Basic Structure
Action | C# ASP.NET Code |
---|---|
Store value | Session["key"] = value; |
Retrieve value | var value = Session["key"]; |
Remove value | Session.Remove("key"); |
Clear all | Session.Clear(); |
End session | Session.Abandon(); |
๐ Example 1: Storing and Displaying Username
โ
Markup File โ SessionDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionDemo.aspx.cs" Inherits="SessionDemo.SessionDemo" %>
<html>
<body>
<form id="form1" runat="server">
<h3>๐ ASP.NET Session Example</h3>
<asp:Label ID="lblPrompt" runat="server" Text="Enter your name:" /><br />
<asp:TextBox ID="txtName" runat="server" /><br /><br />
<asp:Button ID="btnSave" runat="server" Text="Save to Session" OnClick="btnSave_Click" /><br /><br />
<asp:Label ID="lblGreeting" runat="server" Font-Bold="true" ForeColor="Blue" />
</form>
</body>
</html>
โ๏ธ Code-Behind โ SessionDemo.aspx.cs
public partial class SessionDemo : System.Web.UI.Page
{
protected void btnSave_Click(object sender, EventArgs e)
{
// Save input to session
Session["UserName"] = txtName.Text;
// Retrieve from session and display
lblGreeting.Text = "Welcome, " + Session["UserName"];
}
}
๐ฅ๏ธ Output:
[Enter your name: _________] [Save to Session]
โ Welcome, Vaibhav
โ Name is stored on the server and can be used on other pages.
๐ Example 2: Access Session on Another Page
โ
Page 2 โ Greeting.aspx
<asp:Label ID="lblHello" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
lblHello.Text = "Hello again, " + Session["UserName"];
}
โ Works across pages. No form resubmission required.
๐ Classic ASP vs ASP.NET Session โ Key Differences
Feature | Classic ASP | ASP.NET Web Forms |
---|---|---|
Syntax | Session("key") = value | Session["key"] = value; |
Language | VBScript | C# / VB.NET |
State Type | Server-Side | Server-Side |
Session Timeout | 20 mins default | 20 mins default (configurable) |
Clear Session | Session.Abandon() | Session.Abandon() |
Object Support | Stores strings only | Can store any object (int , List<> ) |
Strong Typing | โ No | โ Yes |
๐ ๏ธ Additional Session Features in ASP.NET
Task | Code Example |
---|---|
Check if key exists | if(Session["UserName"] != null) |
Convert to string safely | Convert.ToString(Session["UserName"]) |
Set timeout (in web.config ) |
<sessionState timeout="30" />
| Remove specific item | Session.Remove("UserName");
|
| Clear all session data | Session.Clear();
|
๐ Summary โ Recap & Takeaways
ASP.NET Session State allows you to securely and temporarily store data for each user while they browse your application.
๐ Key Learnings:
- Use
Session["key"]
to store or retrieve any object per user - Data persists across pages until timeout or session end
- More powerful and object-oriented than Classic ASP session
- Great for storing login info, carts, and temporary settings
- Use
Session.Abandon()
to logout or reset
โ Sessions are stored server-side, so no user can see or manipulate them from the browser.
โ Frequently Asked Questions (FAQs)
โ Where is ASP.NET Session stored by default?
โ
In server memory (InProc). It can also be stored in SQL Server, State Server, or Custom Provider.
โ How long does Session last?
โ
Default is 20 minutes of inactivity. This can be configured in web.config
.
โ Can I store a List or DataTable in Session?
โ
Yes! You can store complex objects:
Session["Cart"] = new List<string> { "Item1", "Item2" };
โ Can Classic ASP and ASP.NET share session state?
โ
Not directly. Youโd need a shared database or cookies to bridge session data between the two.
โ How do I logout and destroy session completely?
โ
Use:
Session.Clear(); // Clears all keys
Session.Abandon(); // Ends session and ID
Share Now :