Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use session variable in asp using c#

Tags:

asp.net

how we create session in login page in asp .net with c# give me full example......

like image 721
Adarsh Kumar Singh Avatar asked Mar 12 '11 13:03

Adarsh Kumar Singh


People also ask

What is session variable ASP?

The Session object stores information about, or change settings for a user session. Variables stored in a Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences.

What is the use of session in C#?

Session is a feature in ASP.NET Core that enables us to save/store the user data. Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.


1 Answers

Assuming that your code is in the page (either inline or in the code behind) you can just use...

DataType someValue = (DataType)Session["SessionVariableNameHere"]; //Getter
Session["SessionVariableNameHere"] = someNewValue; //Setter

Obviously you'll need to name your session variable properly, and cast to the appropriate data type when you get it back out of the session.

EDIT - A Full Example

protected void Login1_LoggedIn(object sender, EventArgs e)
{
    Session["LoginTime"] = DateTime.Now;
}

and later in a page load...

protected void Page_Load(object sender, EventArgs e)
{
    Literal1.Text = "Last Online: " + ((DateTime)Session["LoginTime"]).ToString("yyyy-MM-dd");
}
like image 139
Karl Nicoll Avatar answered Oct 05 '22 09:10

Karl Nicoll