---
title: "How to read and write Cookies in ASP.Net"  
description: "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."  
author: "Sachindra Singh"  
published: 2011-02-12  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/448/how-to-read-and-write-cookies-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# 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](https://www.mindstick.com/mindstickarticle/60de760f-be8e-4ad8-8ec8-207aef746ea1/images/ea20bce5-71ed-4381-acab-6a3720c7746b.png)

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](https://www.mindstick.com/mindstickarticle/60de760f-be8e-4ad8-8ec8-207aef746ea1/images/c3584e37-13c2-4332-a2e8-af12808fa3f7.png)

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](https://www.mindstick.com/mindstickarticle/60de760f-be8e-4ad8-8ec8-207aef746ea1/images/3e7cea48-c728-4d3c-bfc7-af897a98349b.png)

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

\

---

Original Source: https://www.mindstick.com/articles/448/how-to-read-and-write-cookies-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
