I have a quite simple problem. I want to create a cookie at a Client, that is created by the server. I've found a lot of pages that describe, how to use it - but I always stuck at the same point.
I have a DBController that gets invoked when there is a request to the DB.
The DBController's constructor is like this:
public class DBController : Controller
{
public DBController()
{
HttpCookie StudentCookies = new HttpCookie("StudentCookies");
StudentCookies.Value = "hallo";
StudentCookies.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(StudentCookies);
Response.Flush();
}
[... more code ...]
}
I get the Error "Object reference not set to an instance of an object" at:
StudentCookies.Expire = DateTime.Now.AddHours(1);
This is a kind of a basic error message. So what kind of basic thing I've forgot?
Responseis null in the controller constructor. It get's set later on. Try your code in an action method.