Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Cookies help please


Message #1 by "Chadrick" <chadm@d...> on Thu, 11 Jul 2002 11:34:46 -0400
How do I check if a cookie exists before I try to Response.Write it? I
have tried to do the following in a short method to no avail.

public string GetCookie(string keyname)
{
	if (Request.Cookies[keyname].Value != "")
		return Request.Cookies[keyname].Value;
	else
		return "";
}


Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Thu, 11 Jul 2002 11:41:30 -0400
You need to check it for nullability:

if (Request.Cookies[keyName] != null &&
Request.Cookies[keyName].Value.Length > 0)
{
}

	You'll want to use the new VB equivalent of && so it doesn't
evaluation the whole statement, but returns as soon as it knows the
condition will fail.

- Chuck

-----Original Message-----
From: Chadrick [mailto:chadm@d...]
Sent: Thursday, July 11, 2002 11:35 AM
To: ASP+
Subject: [aspx] Cookies help please


How do I check if a cookie exists before I try to Response.Write it? I
have tried to do the following in a short method to no avail.

public string GetCookie(string keyname)
{
	if (Request.Cookies[keyname].Value != "")
		return Request.Cookies[keyname].Value;
	else
		return "";
}



Message #3 by "Chadrick" <chadm@d...> on Thu, 11 Jul 2002 11:48:50 -0400
Thanks Chuck

Do you have to create a new HttpCookie object for each cookie you want
to save? Also When I do Response.Cookies.Add("objCookieName") do I have
to add this line for each cookie being saved?


-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...] 
Sent: Thursday, July 11, 2002 11:42 AM
To: ASP+
Subject: [aspx] RE: Cookies help please

You need to check it for nullability:

if (Request.Cookies[keyName] != null &&
Request.Cookies[keyName].Value.Length > 0)
{
}

	You'll want to use the new VB equivalent of && so it doesn't
evaluation the whole statement, but returns as soon as it knows the
condition will fail.

- Chuck

-----Original Message-----
From: Chadrick [mailto:chadm@d...]
Sent: Thursday, July 11, 2002 11:35 AM
To: ASP+
Subject: [aspx] Cookies help please


How do I check if a cookie exists before I try to Response.Write it? I
have tried to do the following in a short method to no avail.

public string GetCookie(string keyname)
{
	if (Request.Cookies[keyname].Value != "")
		return Request.Cookies[keyname].Value;
	else
		return "";
}







Message #4 by Feduke Cntr Charles R <FedukeCR@m...> on Thu, 11 Jul 2002 12:14:33 -0400
Yes, you need to create a new HttpCookie object for each cookie you want to
save, but they can be anonymous:

Response.Cookies.Add(new HttpCookie("key", "value"));

or non-anonymous:

HttpCookie blah = new HttpCookie("key", "value");
Response.Cookies.Add(blah);

Use anonymous methodology unless you specifically need to manipulate the
values of an object later on during the course of the code.  If you were to
.Add(blah) and then change the contents of blah above (without setting it to
a new HttpCookie first) you would find that you modified the original
cookies - not the desired effect - but this is how object variables work.

HTH,
- Chuck

-----Original Message-----
From: Chadrick [mailto:chadm@d...]
Sent: Thursday, July 11, 2002 11:49 AM
To: ASP+
Subject: [aspx] RE: Cookies help please


Thanks Chuck

Do you have to create a new HttpCookie object for each cookie you want
to save? Also When I do Response.Cookies.Add("objCookieName") do I have
to add this line for each cookie being saved?


-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...] 
Sent: Thursday, July 11, 2002 11:42 AM
To: ASP+
Subject: [aspx] RE: Cookies help please

You need to check it for nullability:

if (Request.Cookies[keyName] != null &&
Request.Cookies[keyName].Value.Length > 0)
{
}

	You'll want to use the new VB equivalent of && so it doesn't
evaluation the whole statement, but returns as soon as it knows the
condition will fail.

- Chuck

-----Original Message-----
From: Chadrick [mailto:chadm@d...]
Sent: Thursday, July 11, 2002 11:35 AM
To: ASP+
Subject: [aspx] Cookies help please


How do I check if a cookie exists before I try to Response.Write it? I
have tried to do the following in a short method to no avail.

public string GetCookie(string keyname)
{
	if (Request.Cookies[keyname].Value != "")
		return Request.Cookies[keyname].Value;
	else
		return "";
}









  Return to Index