.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Cookies -Reading and writing

Posted By:Gowthammanju       Posted Date: July 30, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

These code explains about cookies in detail with details of how to read and write a cookie and how to check if a cookie exists.
 

Here it is as

cookie, also known as an HTTP cookieweb cookie, or browser cookie, is used for an origin website to send state information to a user's browser and for the browser to return the state information to the origin site.The state information can be used for authentication, identification of a user session, user's preferences,shopping cart contents, or anything else that can be accomplished through storing text data.



 HttpCookie MyCookie = new HttpCookie("User");

    protected void Page_Load(object sender, EventArgs e)
    {

        HttpCookie myCookie = new HttpCookie("User");
        myCookie["FirstName"] = "Gowtham";
        myCookie["LastName"] = "Manju";
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);

        if (Request.Cookies["User"] != null)
        {
            string firstname="";
            if (Request.Cookies["User"]["FirstName"] != null)
            {
                firstname = Request.Cookies["User"]["FirstName"]; 
            }

            string lastname = "";
            if (Request.Cookies["User"]["LastName"] != null)
            {
                lastname = Request.Cookies["User"]["LastName"];
            }

            Response.Write(firstname + " " + lastname);
        }
        
    }
thats it

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend