articles

Home / DeveloperSection / Articles / How to read and write Cookies in ASP.Net

How to read and write Cookies in ASP.Net

Sachindra Singh18157 12-Feb-2011
What is Cookie?

A Cookie is a small bit of text that store user-specific information requests and pages as they go between the Web server and browser. The Cookie contains information the Web application can read whenever the user visits the site. A Cookie is a small text file that the browser creates and stores on the hard drive of your machine. Cookie is just one or more pieces of information stored as text strings. Following Demonstration showing you that how to create cookies on client system and how to read values of these cookies. In this Demonstration I am storing name, city, age and state of any person.

Code:
publicpartialclass_Default : System.Web.UI.Page
{
    protectedvoid Page_Load(object sender, EventArgs e)
    {
        lbLastV.Visible = false;
     
    }
  
    protectedvoid btnSave_Click(object sender, EventArgs e)
    {
        Response.Cookies["Name"].Value = txtName.Text;//this line will store name of user in Cookie and Cookie name is 'Name'
        Response.Cookies["Name"].Expires = DateTime.Now.AddDays(1);//this line will set Cookie expiration
 
        Response.Cookies["City"].Value = txtCity.Text;
        Response.Cookies["City"].Expires = DateTime.Now.AddDays(1);
 
        Response.Cookies["Age"].Value = txtAge.Text;
        Response.Cookies["Age"].Expires = DateTime.Now.AddDays(1);
 
        Response.Cookies["State"].Value = txtState.Text;
        Response.Cookies["State"].Expires = DateTime.Now.AddDays(1);
 
        Response.Cookies["LastVisit"].Value = DateTime.Now.ToString();
        Response.Cookies["LastVisit"].Expires = DateTime.Now.AddDays(1);
 
        //After click on the save button cookie will create and below code set all textboxes blank
        txtName.Text = "";
        txtCity.Text = "";
        txtAge.Text = "";
        txtState.Text = "";
        lbLastVisit.Text = "";
    }
  
    protectedvoid btnClear_Click(object sender, EventArgs e)
    {
        //When click on the clear button all textboxes will be blank
        txtName.Text = "";
        txtCity.Text = "";
        txtAge.Text = "";
        txtState.Text = "";
        lbLastVisit.Text = "";
    }
    protectedvoid btnRead_Click(object sender, EventArgs e)
    {
        //when click on the read button record will be shown in textboxes
        try
        {
            lbLastV.Visible = true;
            if (Request.Cookies["Name"].Value != null)//if condition checking Cookie(Name) has value or not
                txtName.Text = Request.Cookies["Name"].Value;
            if (Request.Cookies["City"].Value != null)
                txtCity.Text = Request.Cookies["City"].Value;
            if (Request.Cookies["Age"].Value != null) txtAge.Text = Request.Cookies["Age"].Value;
        if (Request.Cookies["State"].Value != null)
                txtState.Text = Request.Cookies["State"].Value;//this line will get value from Cookie which name is Name and set in the txtState texbox
            if (Request.Cookies["LastVisit"] != null  lbLastVisit.Text = Request.Cookies["LastVisit"].Value
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert("+ex.Message+")</script>");
        }
    }
}

 

How to read and write Cookies in ASP.Net

After filling all text fields click on the Save button. Cookies will be created on the client machine and all textboxes will be blank as shown below:

How to read and write Cookies in ASP.Net

If we want to read Cookies then click on the Read button than all record will be shown in textboxes as shown below:

How to read and write Cookies in ASP.Net

Clear button will clear all textboxes and Last Visit label from the page.



Updated 04-Mar-2020

Leave Comment

Comments

Liked By