Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Can anyone see why my bigCookie, "cookieMonster", is not being set?
public void Page_Load (object sender, System.EventArgs e) {
string strUserId = "jdoe";
string strName = "John Doe";
string strDate = DateTime.Now.ToString();
HttpCookie bigCookie = Request.Cookies["cookieMonster"];
if (IsPostBack) {	// Page Loads for first time
Label1.Text = "Is Postback";
} else {			 // User Clicked a button
Label1.Text = "Is Not Postback";
}
if (bigCookie == null) {
Label2.Text = "bigCookie = null";
bigCookie = new HttpCookie("cookieMonster");
bigCookie.Values.Add("userId", strUserId);
bigCookie.Values.Add("name", strName);
bigCookie.Values.Add("lastVisit", strDate);
bigCookie.Expires = DateTime.Now.AddDays(365);
bigCookie.Path = Server.MapPath("/");
Response.Cookies.Add(bigCookie);
} else
if (bigCookie["userId"].ToString() == "") {
Label2.Text = "bigCookie = (blank)";
bigCookie = new HttpCookie("cookieMonster");
bigCookie.Values.Add("userId", strUserId);
bigCookie.Values.Add("name", strName);
bigCookie.Values.Add("lastVisit", strDate);
bigCookie.Expires = DateTime.Now.AddDays(365);
bigCookie.Path = Server.MapPath("/");
Response.Cookies.Add(bigCookie);
} else {
Label2.Text = "bigCookie value set";
strUserId = bigCookie["userId"].ToString();
strName = bigCookie["name"].ToString();
strDate = bigCookie["lastVisit"].ToString();
}
}

Label2.Text is always "bigCookie = null" whether IsPostBack is true or not, but I don't know how I should rewrite the code to make the changes. Am I writing this cookie the wrong way? Am I reading it in the wrong way? Any suggestions???

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...