Cookie Error when run web page test.aspx C#.net
Hi every body,
My code as following:
private void Page_Load(object sender, System.EventArgs e)
{
HttpCookie myCookie = new HttpCookie("city_id");
myCookie = Request.Cookies["city_id"];
int city_id = 1;
if(myCookie != null)
{
Session.Add("city_id",myCookie.Value);
city_id=int.ParsemyCookie.Value.ToString());
}
else
{
// Set the cookie value
myCookie.Value = "1";
// Add the cookie
Response.Cookies.Add(myCookie);
Session.Add("city_id",myCookie.Value);
}
}
When I build no errors. but I run it occur error as following:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 40: {
Line 41: // Set the cookie value
Line 42: myCookie.Value = "1";
Line 43: // Add the cookie
Line 44: Response.Cookies.Add(myCookie);
Source File: e:\ndv4\test.aspx.cs Line: 42
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Why is it occur that? How is fix this error?
Thank you so much,
Best regards.
Minh Phan.
|