user1509
user1509

Reputation: 1161

NullReferenceException in Cookies

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?

Upvotes: 0

Views: 738

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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.

Upvotes: 2

Related Questions