Cookies in ASP.NET

How to Create a Cookie ?
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename,cookievalue);
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires =dtExpiry;

- How to Read Cookie ?
 string value= Request.Cookies[cookiename].Value;
 - How to Delete a Cookie? 
 Response.Cookies[cookiename].Expires = DateTime.Now.AddYears(-30);
- Checking for Cookie in ASP.NET?
if (Request.Cookies[cookiename] == null)

TextBox2.Text = "No cookie found";

else

TextBox2 .Text = Request.Cookies[cookiename].Value;

- How to Avoid HttpExcepiton while Accessing with Session Variable ?
Generally, for user login

if Session["user"] is there then proceed further else redirect to homepage .. for this most commonly we will chek it as

if (Session["user"]!=null )
{
}

This will throw exception , Instead try this


if(Convert.ToBoolean(Session["user"]!=true)
{
       Response.Redirect("login.aspx");
}

Like