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

How to read and write Cookies in ASP.Net


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 12757  View(s)
Rate this:

How to read and write Cookies in asp.Net

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:

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        lbLastV.Visible = false;

     

    }

  

    protected void 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 = "";

    }

  

    protected void 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 = "";

    }

    protected void 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.


Don't want to miss updates? Please click the below button!

Follow MindStick