Home > Asp.net > Difference between Application and Session variable

Difference between Application and Session variable

Application and Session variables store values that are global rather than page-specific for either a particular user (the Session) or to all users (the Application). Both the variables store temporary data that an application stores between a user’s visits to the web application. They are stored on the server. Client browsers are then attached to the session through a cookie. As a result, the client must have cookies enabled in the browser for Session and Application variables to work. Let’s dig deeper into these variables,

Application variable
Application variables remain common for the whole application for all the users.
Variable’s value can be used across the whole application by any user.
When an Application variable is created, the variable’s data exists only once for that application on the web server.
The script requires only a small amount of server memory to hold the definition for the data source.
Application variable dies when application stops or killed forcefully.
The ideal example is site counter i.e to find out the number of people accessed a particular site since the time it went live via application variables and incrementing the same on ever session start.

Set the application variable to a value:
Application[“Name”] == “abc”;

Session variable
Session variables remain common for the whole application but for one particular user.
They can be used across the whole application but each user will have a copy.
The server creates a new Session object for each new user, and destroys the Session object when the session expires or when it is killed forecibly.
Common information are stored in session variables like name, id, and preferences.
Session variables consume memory resources, so they can force a web server to run out of memory, depending on what’s stored in the variables and how many users visit the application at approximately the same time. When a Web server is low on memory, paging starts and performance diminishes.

The ideal example is storing user id or user name. If we want to show name of the logged in user on every page, then store the user name in session and can be shown on every page. Check the example below:

Set session variable from Textbox control:
Session[“UserName”] = UserNameTextBox.Text;

On ASPX page , get session value:
string loggedInUsername = Session[“UserName”] as string;

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">