Support us MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

Write and Read a cookie - ASP.NET Interview Questions


Question:Give an example to show how to write and read a cookie from a client's computer.

Answer:
1. The following example shows how to write a "USER" cookie to a client's computer. The "USER" cookie, stores
FirstName
LastName
LastVisit


2. Create the user interface to enter FirstName and LastName. The HTML for the webform is as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CookiesExample.aspx.cs" Inherits="CookiesExample" %>
<html>
<head runat="server">
<title>Write a cookie to the client computer</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td style="width: 100px">
First Name</td>
<td style="width: 100px">
<asp:TextBox ID="FirstNameTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px">
Last Name
</td>
<td style="width: 100px">
<asp:TextBox ID="LastNameTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:Button ID="WriteCookieButton" runat="server" Text="Write Cookie" OnClick="WriteCookieButton_Click" />
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:Button ID="ReadCookieButton" runat="server" Text="Read Cookie" OnClick="ReadCookieButton_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>

3. WriteCookieButton_Click event handler in the code behind file, has the code required to write the cookie to the client computer as shown below.
protected void WriteCookieButton_Click(object sender, EventArgs e)
{
// Create an instance of HttpCookie class
HttpCookie UserCookie = new HttpCookie("USER");
// Populate FirstName, LastName and LastVisit fields
UserCookie["FirstName"] = FirstNameTextBox.Text;
UserCookie["LastName"] = LastNameTextBox.Text;
UserCookie["LastVisit"] = DateTime.Now.ToString();
// Set the cookie expiration date
UserCookie.Expires = DateTime.Now.AddDays(3);
// Write the cookie to the client computer
Response.Cookies.Add(UserCookie);
}

4. ReadCookieButton_Click even handler in the code behind file has the code to read the cookie from the client computer as shown below.
protected void ReadCookieButton_Click(object sender, EventArgs e)
{
// Check if the "USER" cookie exists on the client computer
if (Request.Cookies["USER"] != null)
{
//Retrieve the "USER" cookie into a cookie object
HttpCookie UserCookie = Request.Cookies["USER"];
//Write FirstName,LastName and LastVisit values
Response.Write("First Name = " + UserCookie["FirstName"] + "
");
Response.Write("Last Name = " + UserCookie["LastName"] + "
");
Response.Write("Last Visit = " + UserCookie["LastVisit"] + "
");
}
}

5. Finally test. Run the application and enter first name and Last name and click, the write cookie button. This should write the cookie to the client's computer. Now click the read cookie button, which will read the FirstName, LastName and LastVisit information from the cookie and writes on to the webform.

7 comments:

  1. Thanks...
    a nice and compact example...

    ReplyDelete
  2. Thanks...
    a nice and compact example...

    ReplyDelete
  3. Pankaj Manohar UnhaleJuly 10, 2009 at 5:21 AM

    Thanks Venkat,
    You did a very good job

    ReplyDelete
  4. Thanks for adding this code. please keep adding such useful code. it really helped me as it's really easy to understand. it boosted my confidence..thanks once more !!! :-)

    ReplyDelete
  5. Nice..... good resourse....

    ReplyDelete
  6. Thank you, Venkat
    In one-two days I learn more about ASP.net then for the last 6 month.
    You created a great help - website!!

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us