how can we use the session object in asp.net

Dani AI

Generated

Short answer: yes — ASP.NET session state lets you keep data available across pages without explicitly passing it. As moved the thread correctly to ASP.NET, and pointed out the basic idea, here is a small, practical pattern and what to watch for.

// Login.aspx.cs (after validating credentials)
Session["UserId"] = userId;   // store a small identifier
Response.Redirect("Home.aspx");

// Home.aspx.cs (on the next page)
object raw = Session["UserId"];
if (raw == null) Response.Redirect("Login.aspx");  // session missing -> force login
int userId = (int)raw;  // safe cast after null check
// use userId to load current user data from DB

Troubleshooting and best practices: always null-check before casting; store only small values (IDs, roles), not big objects; if you use StateServer or SQLServer modes your objects must be serializable; InProc sessions are lost on app restart or across web-farm nodes (use StateServer/SQLServer or sticky sessions); clients blocking cookies will break default cookie-based sessions (or enable cookieless mode); default timeout is 20 minutes (changeable in web.config). For security, do not put passwords in Session and consider regenerating the session id on login (Session.Abandon or SessionIDManager techniques) to reduce fixation risk. If you want examples for VB.NET, web.config changes, or web-farm setups, tell us which environment you are using.

Recommended Answers

All 2 Replies

I moved your post from JSP to ASP.NET section as these two technologies are very different. So solution from JSP will not work for in ASP.NET

u just declare session["name"] ="value"in first page,and in next page u get the value for session["name"]="value",u need not pass any thing from one page to another,try it u r self
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.