0

I have the following code:

HttpCookie myCookie = new HttpCookie("PopMsgText");
myCookie.Value = message;
Response.Cookies.Add(myCookie);

It is giving NullReferenceException at the Response line. What could be the reason?

user1509
  • 1,151
  • 5
  • 17
  • 45

1 Answers1

2

Either the Cookies property or the Response object is null. This could happen depending on where you are calling this code. For example if you attempt to access the Response object in a background thread it might be null because there's no HttpContext associated to background threads.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928