Updated March 30, 2023
Introduction to SessionID
Everybody must have visited any of the websites on the browser. So, when we visit any website it means it is sending a request which is HTTP (Hyper Text Markup Protocol). As HTTP is a stateless request, each single HTTP request will be treated as an independent request by the web server. An ASP.NET session will easily identify request sent from the client side of the same browser. This session has unique ID by which it is uniquely identify a browser with the help of session data on the server. This SessionID value is randomly generated value by the ASP.NET and will be stored in session cookie in the browser only which is non-expiring.
Syntax
Let’s see the syntax of SessionID below which shows us how to store the username in session.
Session["UserName"] = txtUser.Text
How we can Retrieve the Values from Session?
//checking for the session whether null or not
if (Session["UserName"] != null)
{
// UserName is Retrieved from Session
lblWelcome.Text = "Welcome to EDUCBA : " + Session["UserName"];
}
else
{
//Do Something else
}
We can also store the dataset in session. Let’s see the code or syntax for it.
//Storing dataset on Session
Session["DataSet"] = _objDataSet;
How to Create a Session ID?
Let’s see how to create that is store and retrieve the session id. Below code is showing us how to create an ASP.NET page for the employee Name and Email id of a user which will set them to retrieve values from TextBox controls from Button Click event
For storing the values in Session ID below code can be written:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Session["EmployeeName"] = txtEmployeeName.Text;
Session["EmaiIDl"] = txtEmaiIDl.Text;
Response.Redirect("http://localhost/SessionIDDemoe/employeeDetail.aspx");
}
For retrieving the value:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["EmployeeName"] != null && Session["EmailID"] != null)
{
string name = Session["EmployeeName"].ToString();
string email = Session["EmailID"].ToString();
Response.Write(string.Format("My name is :{0} and Email id is :{1}",name,email));
}
}
It is not needed to declare a session variable to the collection.
This code will allow an application to stores a value in a particular session and redirects or sends it to other page after clicking the Button.
Examples
we can have look on below diagram for better understanding that how session deals with web server and client.
- We are having two clients and one webserver. Suppose we visit two different pages let’s say one is “home” page and other is “company info” of a website.
- Suppose first we go to the company’s info page that means we request first HTTP REQUEST and get the information.
- After that when we navigate to other “home” page, it will create another request which is also HTTP request.
- So it means now we are having two HTTP request that also from the same client that is web browser.
- However, both the request is independent as they are dealing with different pages.
- As the web server doesn’t recognize who is visiting, it’s the main problem that if we store values on one page and now we want to retrieve it on other page.
- So the solution to this program is ASP.NET session.it will stores the value and helps us to retrieve it on the other page for the user.
- Session of ASP.NET will identify the request which is from the same browser.
- It will then provide a way to continue variable values for the limited time window which is session.
What happens internally is when user sends a request and depending upon the request server sends response but after that it will forgot who the user is. He doesn’t know anything about the user. So next time when same user comes, the server treats it as a new request/new user as it forgot what exactly user did previously. Every time when we hit the button, request is sent and taken as a new request.
Server always generate a unique session ID for its each session. This ID is stored in memory of the user in the form of Cookie. This cookie consists of set of characters which may look like “kjdksja2323kjdkomudzaq”. Servers are used to store the session data which may be store in SQLServers. Suppose if cookies are disabled then what will happen. So in this case session id are attached with the URL.
Example of ASP.NET SessionID
URL with Cookie: http://www.educba.com/page.aspx/(s(kjdksja2323kjdkomudzaq))page.aspx
URL without Cookie: http://www.educba.com/page.aspx
However, the method with url cookie is not safe and good as user can save this URL as a bookmark which can create problems. Session value depends upon the cookies. Session is the server side and cookies are the client side management. Whenever we store any value in the session that value is stored in the server whereas session id is stored on the client side machine in the form of cookie. Whenever we try to fetch out the values from session we have to send the session id from the cookie the header request then only we can fetch the value. This session id is sent internally.
If the user clears the cache and cookie of the browser, then we will not be able to get the session value. once we will clear the cookies, session id gets also be cleared automatically and will not be able to find the key by which we cannot get the value from server even if the session is there on the browser.
Advantages of Sessions
- It provides and maintains the user data and its setting individually.
- We can store value type and reference type data in a session.
- Storing and retrieving the values are easy.
- It is server-side not a client-side so it is more secure. As it stores on a server-side it needs more memory.
Conclusion – ASP.NET SessionID
I hope this article explains the ASP.NET sessionId in detail. With the help of example and syntax, it is very clear how we can create a session. So we can say that Session can store the uniquely identified value on the server which is known as SessionID. It is best state management technique as it stores the value as client-based.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET SessionID” was beneficial to you. You can view EDUCBA’s recommended articles for more information.