Powered by Blogger.

Thursday, March 13, 2014

Tutorial on Cookies in asp.net




Here we will discuss what is a cookie, as well as the types of cookies available in asp.net. You can also:

- Exception handling in C#.Net

- How to get body height using jQuery in Asp.Net?

- How to handle exception in sql server stored procedure?

- A cookie can be defined as small amount of memory used by the web server within the client system.

- The cookie are requested to maintain personal information of the client. Information like user name, no of visits, last visited date etc are get stored.

There are 2 types of cookies:
1. Inmemory cookie
2. Persistant cookie

Inmemory Cookie:
- When the cookie is maintained within browser process memory then it is called as inmemory cookie. This is very much temporary which will be erased when the browser is closed.

Create cookie:
HttpCookie obj = new HttpCookie("nameofcookie");
obj.Value = "100";
Response.AppendCookie(obj); //Here cookie will be send to the browser.

Reading cookie:
HttpCookie obj;
obj = Request.Cookies["nameofcookie"];
if(obj !=null)
{
string value = obj.Value; //This will return the value.
}

Persistent cookie:
- When the browser maintains cookie on to harddisk memory then it is called as persistant cookie.
- This will have a life time, when the cookie will earse.

Create cookie:
HttpCookie obj = new HttpCookie("nameofcookie");
obj.Value = "100";
obj.Expires.AddDays(5);//Cookie will expire after 5 days
Response.AppendCookie(obj);

Reading cookie:
HttpCookie obj;
obj = Request.Cookies["nameofcookie"];
if(obj !=null)
{
string value = obj.Value; //This will return the value.
}



0 comments

Post a Comment