Using Session in ASP.NET

Session is commonly used to store data for per user who is logged in or using website. Common use of Session can be storing username, UserID who is logged in to the website. 

Session values can be accessed from any page within the website. Below is the code how we can store and get value from Session.

Storing Value in Session

Session["UserName"] = "Mairaj Ahmad";

Getting value from Session

string UserName = string.Empty;
if (Session["UserName"] != null)
{
   UserName = Session["UserName"] as string;
}

Session is an Object so we need to convert the session value to the type which we have stored when getting value from session.


Here we stored a string value into session so when getting value from session we need to convert the value of session in string.

Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net