public void CreateCookie()
{
HttpCookie myCookie = new HttpCookie("TestCookie");myCookie.Values["Val1"] = "Value 1 test";
myCookie.Values["Val2"] = "Value 2 test";Response.Cookies.Add(myCookie);
}
on another page I have this;
public void ReadCookie()
{
HttpCookie myCookie = Request.Cookies["TestCookie"];string Val1;
string Val2;Val1 = myCookie["Val1"];
Val2 = myCookie["Val2"];
}
But I am getting this error:
Object reference not set to an instance of an object.
On this line:
Val1 = myCookie["Val1"];
Anyone know what this could be? Thanks
KeithMy friend said it had to do with custom HTTP Headers in IIS. Anyone know about this, or if I am doing something wrong in my code?
public void CreateCookie()
{
Response.Cookies["val1"].Value = "val 1 test";
Response.Cookies["val2"].Value = "val 2 test";
}public void ReadCookie()
{
string Val1 = Request.Cookies["val1].Value;
string Val2 = Request.Cookies["val2].Value;
}
That is still causing the same error:
Object reference not set to an instance of an object.
If I create and and read the cookie on the same page, it works fine.. But if I Create the cookie on lets say, the "Home Page", when the user goes to another page, I get that error. Any other suggestions?
Are both pages located in the same web application? Also the cookies might be expiring.
Yes they are in the same Application. Let me set an extended expiration date to see if that fixes anything. Thanks for the response!
I am still getting that error.. I have tried this:
public void CreateCookie(){
HttpCookie myCookie = new HttpCookie("TestCookie");myCookie.Values["Val1"] = "Value 1 test";
myCookie.Values["Val2"] = "Value 2 test";myCookie.Expires = DateTime.Today.AddDays(1);
Response.Cookies.Add(myCookie);
}public void ReadCookie1(){
HttpCookie myCookie = Request.Cookies["TestCookie"];string Val1, Val2;
Val1 = myCookie["Val1"];
Val2 = myCookie["Val2"];
}public void ReadCookie2(){
string Val1, Val2;Val1 = Request.Cookies["TestCookie"].Values["Val1"].ToString();
Val2 = Request.Cookies["TestCookie"].Values["Val2"].ToString();
}
Both of the ReadCookie methods throw the same error. And I made sure my browser has cookies enabled. This should be so simple, never have had this many problems with a simple cookie! Thanks for your help guys.
Keith
If I add everything to one method, like this:
public void ReadWriteCookie(){//write
HttpCookie myCookie = new HttpCookie("TestCookie");
myCookie.Value = "Test";
Response.Cookies.Add(myCookie);//now read
HttpCookie readCookie = Request.Cookies["TestCookie"];
string cookieVal = readCookie.Value;
}
that doesn't throw the error.. but if I create the cookie, then try to read it from a different page of the web application, I get the error.
I am currently using this to write and read cookies in my application and it works fine, hopefully this helps.
WRITE THE COOKIE!
HttpCookie YourCookieName= new HttpCookie("val1");
DateTime now = DateTime.Now;
// Set the cookie value.
CompanyGuardianGuidCookie.Value = "your value here";
// Set the cookie expiration date.
CompanyGuardianGuidCookie.Expires = now.AddMinutes(5);
// Add the cookie.
Response.Cookies.Add(YourCookieName);
READ THE COOKIE!
HttpCookie YourCookieName= new HttpCookie("val1");
YourCookieName = Request.Cookies["val1"];
as i said, it works for me and it causes no errors.
Let me know if it works for you
Also check to see the Page_Load event is something like this:
Page_Load()
{
if(!Page.IsPostBack)
{
// do something or nothing
}
}
in the write cookie, i kinda screwed up and left an old tag from my original code, here is tha update
HttpCookie YourCookieName= new HttpCookie("val1");
DateTime now = DateTime.Now;
// Set the cookie value.
YourCookieName.Value = "your value here";
// Set the cookie expiration date.
YourCookieName.Expires = now.AddMinutes(5);
// Add the cookie.
Response.Cookies.Add(YourCookieName);
I don't think you guys are following, look at this;
if(!Page.IsPostBack)
{
//write, add, read cookie all at once
HttpCookie myCookie = new HttpCookie("TestCookie");myCookie.Value = "This is a test cookie";
myCookie.Expires = DateTime.Today.AddDays(1);Response.Cookies.Add(myCookie);
lblmyCookie.Text = Request.Cookies["TestCookie"].Value;
}
else
{
//when page submits, attempt to read cookie
HttpCookie myCookie = Request.Cookies["TextCookie"];lblmyCookie.Text = myCookie.Value;
}
Ok, the first part. It writes the cookie, adds it, then it reads it.. Just like your example, and it works perfect.. I have one button on the form, when you submit it, it attempts to read the cookie, and again it throws the error. In one swift motion, I can easily write/read the cookie. But if I store it, and attempt to read the cookie after a postback, or on another page, it does NOT work.
Object reference not set to an instance of an object.
this line:
lblmyCookie.Text = myCookie.Value;
Is the problem that you're referring to TestCookie in the first portion of the if and TextCookie in the else or was that just a typo in your post?
0 comments:
Post a Comment