Thursday, March 22, 2012

Trouble with Response.Cookies

In Visual Studio.NET, I can't get the following code to compile:


public static bool setCookie(string cookiename, string cookievalue)
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename,cookievalue);
}
catch( Exception e)
{
return false;
}
return true;
}

Visual Studio returns the following build error:

'System.Web.UI.Page.Response' denotes a 'property' where a 'class' was expected.

Any advice would be appreciated.

DougNot sure what the problem is. Have a feeling it is because you have to set the cookie's value before adding it.

As MS put it:
HttpCookie MyCookie = new HttpCookie("LastVisit");
DateTime now = DateTime.Now;

MyCookie.Value = now.ToString();
MyCookie.Expires = now.AddHours(1);

Response.Cookies.Add(MyCookie);

Have a look atthis for examples

HTH

Jagdip
Okay I tried rearranging the code per the suggestion, but still get the same error. Here's the updated code:


public static bool setCookie(string cookiename, string cookievalue)
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
objCookie.Value = cookievalue;
Response.Cookies.Add(objCookie);

}
catch( Exception e)
{
return false;
}
return true;
}


try this:
httpcontext.current.response.cookies.add(objcookie)

0 comments:

Post a Comment