Remember Me
forgot your password?

How to Choose From Viewstate, Session, Application, Cache, and Cookies

 

Problem with Web Applications

Web Applications are natively statesless, means once a web page renders from server to client, nothing remains on server and the next time user submits the page you have to load all values and create the page again.

 

ASP.NET provides multiple simple solutions to this problems like:




  • Viewstate



  • Session Variables



  • Application Variables



  • Cache



  • Cookies




 

Now the question arises that when to use what?

 

1- Viewstate

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose "EnableViewstate" property is "true".

You can also explicitly add values in it, on an ASP.NET page like:

Viewstate.Add( "TotalStudents", "87" );

Viewstate should be used when you want to save a value between diferent roundtrips of a single page as viewstate of a page is not accessible by another page.

Because Viewstate renders with the page, it consumes bandwith, so be careful to use it in applications to be run on low bandwith.

 

2- Session Variable

Session variables are usually the most commonly used.

When a user visits a site, it's sessions starts and when the user become idle or leave the site, the session ends.

Session variables should be used to save and retrive user specefic information required on multiple pages.

Session variables consumes server memory, so if your may have a huge amount visiters, use session very carefully and instead of put large values in it try to put IDs and references

 


3- Application variables

Application variables are shared variables among all users of a web application

Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications

Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.

 


4- Cache

Cache is probably the least used state feature of ASP.NET.

Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.

Cache should be used or frequently used pages, controls, and data structures

Data cache can be used to cache frequently used list of values e.g. list of products



6- Cookies

Cookies are some values saved in browsers by the website to retrivbbe and use afterwards.

Usually cookies are used to help dynamic websites to identify visitors and retrieve their saved preferences.

Cookies are also used to facilitate auto login by persisting user id in a cookie save in user's browser.

Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.

 


Finally remember the following points on your finger-tips:




  • Viewstate is bandwidth hungry



  • Session variables are memory hungry as per number of users



  • Applications variables are shared



  • Cache is memory hungry as per number of resources



  • Cookies are the least secure

Ahmed Siddiqui

Seasoned Microsoft Certified Professional having seven years of experience in analysis, design, and development of enterprise applications; utilizing true world class software development patterns & practices.

Rate this Article: 4.3 / 5 stars - 26 vote(s)
Print Email Re-Publish

Add new Comment



Captcha
0
1. Paulo Zemek (23:06, 16.02.2009)
I have created a ViewState alternative that I really want to sell, but for now I am willing to give it for free as an way to make some advertisement.
But, I also want to explain why I develeped the solution.

When I started in a new job, the site was getting out-of-memory almost every week. The problem was the use of session. Considering only "memory" and "bandwidth" consideration, the developers made more than one error.
First, large datasets where stored in session, so they were not sent to the client. That's why the server memory was filled very fast. Using ViewState killed performance, and reading the datasets everytime also killed performance and complicated the programming.
The first version of my caching structure only knew how to Serialize information in temporary files and retrieve them from that temporary files. So, instead of putting Session["item"] = item, we need to do: Session["item"] = new Cache(item);
This solved the memory problem. But, the mix of session and viewstate caused much more problems than that.

A very simple example (that does not even need to use ViewState, only session).
Create a page that receives an image and shows that image to the user before he confirmates it.
Then, to avoid sending the image from client to server again, you store the received image on Session.
If now, the user press Confirm. You confirm that image and everything is ok.
But, now try:
Load the selection page.
Press Ctrl+N to open a new window in the same page.
Go to the first window again, and choose image1.jpg
Go to the second window and choose image2.jpg
And now you go back to the first window and confirm. You end-up confirmating image2.jpg, as that was the value in the session.
If you try to use different image names for session, then you will end-up killing server memory even faster.

My solution, then, was to create a viewState alternative. I called it State.
It works exactly like ViewState (as it is by page). But, it only sends a reference to the client.
All the information is saved into the disk, using a sort of "hashset" for serialized buffers. If two buffers are identical, only one file is created. And the files are killed from time to time (I use 4 hours, but that is configured).
But, the best:
From 16giga-byte (and going out of memory) the server now uses only 2giga when it is really busy. And the files rarelly occupy more than 300mb. After all, the buffers in general are reutilized.

So, before using Session or ViewState by bandwidth/Memory usage, think also about how exclusive that information may be. Opening a new tab on the same page can impact the results?

If someone want to see my solution, send me an e-mail:
paulozemek@hotmail.com
+1
2. Patricia Nelson (16:41, 10.09.2008)
Thank you. I've been looking for a simple decision tree to help me design my first ASP.Net application. This was perfect
0
3. Auther (08:13, 12.09.2008)
Thanks for the feedback Nelson.
0
4. rajesh modi (07:29, 11.01.2009)
hi sorry i m not able to understyand cookies,viewstate,session and application.so plz give details with some perograms.

  • Latest Programming Articles
  • More from Ahmed Siddiqui

PassQuick Nortel 920-146 Study Guide

By: calomi | 09/11/2009
There are so many websites on internet that are providing knowledge, study material and information about the Nortel 920-146 certifications, but there is one problem that the information that is provided by these websites is not of high quality and up-to-the-mark. Passquick is one website that provides you related, high quality and up-to-the-mark information, knowledge and study material about Nortel certifications.

Testinside Cisco 640-861 exam

By: calomi | 09/11/2009
Cisco 640-861 exam is a very valuable exam of Cisco certification. This exam is one of the most important and top of the line certifications for the IT professionals. Cisco 640-861 exam is basically associated with the Troubleshooting, and so this certification exam that is basically connected with the Cisco 640-861 certification.

Typecasting and its importance in C++

By: james edward | 08/11/2009
Type Casting causes the program to treat a variable of one type as though it contains data of another type. Read brief tutorial on typecasting in c++

How to Setup Your Own Blog with WordPress

By: Felix Gomez Jr | 08/11/2009
Have you tried to setup wordpress and have met some problems you were not able to solve until now? This article might help.

An Event Registration Service Can Eliminate Much Event Planning Stress

By: Brad Robert | 06/11/2009
Event-Registration-Service.com is the leading online event registration, event planning and event management software for all types of businesses and organizations.

HTML Guestbook in ASP.Net

By: pons_saravanan | 05/11/2009
A very simple guest book without any Database storage.

Himfr.com reports the new sail will be put on the market in January next year

By: chenxiao | 05/11/2009
the new sail will be put on the market in January next year.

iPhone Applications at Its Best

By: Arun Kumar | 05/11/2009
The Apple iPhone has been deemed the gadget of the decade. It’s such kind of a power product from the house of Apple that has really changed the way people communicate. In the last few years we have seen several mobile handsets, PDAs and pocket PCs from the big labels, but the wonder that Apple has done is really something special, something beyond everyone’s imagination.

Why Patterns Suck?

By: Ahmed Siddiqui | 16/06/2008 | Programming
This article is about the extremism in implementing Software Design Patterns and the inability to distinguish Pattern and its application.

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.05, 1, w1)