Hi,

Is there any way of storing some variables, say username for a particular session in ASP.NET.

E.g, If we let a user login in a page, then I want him to stay logged in for all the pages he traverses on the server.

Currently, I am trying Server.Transfer and the Hashtable() methods to retrieve the value and use it again in other pages.

Is there any better method than this?

thanks in advance,

Regards,

Abhijit

Use session state. That'll hold the variable until the user ends the session or it times out.
To set it:
Session.Add(name, value)

To get it:
x = Session.Item(name)

Hope that helps,

Cool..

I will try it out. But will I be able to use the value of x, in functions other than ASP. I am using C# as the base language, so, will I be able to use the value of x, say, in a query or to pass to a function?


Regards,

Abhijit

Also,

Unless we predefine the timeout, the session should last until the user closes the browser. Am I correct?


Thanks and Regards,

Abhijit

Session timeout is the amount of time, in minutes, allowed between requests before the ASP.NET session-state provider terminates the session.

It means that, if you set the session timeout for 20 minutes, the session wil be timed out if your browse makes a request on or after 21st mintue (from the last postback/submit).

Also if you close the browser before 20 minutes from the last postback, actually the session timeout will occur after 20 minutes from the last postback.

You can store variables/business objects in Session and retrive it among different pages of your web application.

Alternate c# syntax for session I use: Session["userName"] = Textbox_userName.text; or

if (Session["userName"] == null)
  server.transfer("LoginPage.aspx");
else
  Label_userName.Text = "Welcome " + (String)Session["userName"];

Session is just like hastable, a collection of objects, one per user. Though you can store any object, on retrieval you will need cast it. Also try to keep minimum stuff in session.

Mark th thread closed if your problem is solved.

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.