how to use session in asp.net

/
0 Comments

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application.

Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property. In an ASP.NET page, the current session variables are exposed through the Session property of the Page object.

Assign value into Session

Session["User"] = "Rajesh";

Description :
It will assign the string value into session with key name "User".

Retrieve value from Session

string user = Session["User"].ToString();

Description :
It will retrieve the value from session with key name "User".

Remove Particular Session

Session.Remove("User");

Description :
It will remove the value from session with key name "User".

Assign custom object into Session

custom object into Session

Description :
It will assign the employee class into session with key name "EmployeeInfo".

Retrieve/Convert custom object from Session

Convert custom object from Session

Description :
It will retrieve the employee class from session with key name "EmployeeInfo".

Using session for get/set like property

public static string UserName
{
get { return session["username"] as string; }
set { session["username"] = value; }
}

Description :
It will assign and retrieve the value with key "username" and it will work as property or member in the page



You may also like

No comments:

Powered by Blogger.