Microsoft Visual Studio 2017 Promo Code
Microsoft Office 2016 Promo Code

State Management using QueryString, Cookies, ViewState, Session and Application in ASP.NET


State management is used to maintain user state throughout the application. Managing state in a stand-alone application is trivial as they don't have a request/response nature. Information you want to maintain over the lifetime of a web applications depends on your requirement.


The following are the commonly used state management techniques.

·         QueryString

·         Cookies

·         ViewState

·         Session state

·         Application state


Query String:


This is simple and efficient way of maintaining information across requests. The information you want to maintain will be sent along with the URL as shown below.




//Reading values from query string

string name = Request.QueryString["name"];

string location = Request.QueryString["location"];


Query string will not consume any server resources and very easy to use and it is the most efficient state management technique. However, it has many disadvantages.

·         You can pass information only as a string.

·         URL length has limitations. So you can't send much information through URL.

·         Information passed is clearly visible to everyone and can be easily altered.




These are nothing but small files that are created and stored on the client's system or on client's browser memory.
Cookies are managed by the browser and will take care about removing expired cookies. If you need to remove a cookie before the expiry period, you have to create a cookie with the same name and with an expiry date that is already passed. This will make browser think that the cookie is expired and will be removed immediately.


·         Cookies have a size limitation of 4KB. Storing huge information is not possible.

·         Cookies can be easily tampered as they are kept in the client's machine. So additional security checking has to be done when using them.

·         The user can disable cookies.


View State:


View state is another client side state management mechanism provided by ASP.NET to store user's data, i.e., sometimes the user needs to preserve data temporarily after a post back, then the view state is the preferred way for doing it. It stores data in the generated HTML using hidden field not on the server.

View State provides page level state management i.e., as long as the user is on the current page, state is available and the user redirects to the next page and the current page state is lost. View State can store any type of data because it is object type but it is preferable not to store a complex type of data due to the need for serialization and de-serialization on each post back. View state is enabled by default for all server side controls of ASP.NET with a property EnableviewState set to true.


           protectedvoid Page_Load(object sender, EventArgs e)


                  if (IsPostBack)


                            if (ViewState["count"] != null)


                                    int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;

                                    Label1.Text = ViewstateVal.ToString();

                                    ViewState["count"] = ViewstateVal.ToString();




                                   ViewState["count"] = "1";




        protectedvoid Button1_Click(object sender, EventArgs e)


            Label1.Text = ViewState["count"].ToString();



Session State:


Session state is another state management techniques in which Data stored in session will be kept in server memory and it is protected as it will never get transmitted to a client. Every client that uses the application will have separate sessions. Session state is ideal for storing user specific information.


//Storing and accessing data from session.

 Session["name"] = "Joe";

 string name;

 name =Convert.ToString(Session["name"]);



Application State:


It provides methods for storing information which can be accessed globally. Information stored on application state will be available for all the users using the website. The following code shows storing a value in an application variable and reading from it.



Application["pageTitle"] = "Welcome to my website";



 string pageTitle;

 if (Application["pageTitle"] != null)

        pageTitle = Application["pageTitle"].ToString();


Add comment


© Copyright 2018 | Sitemap