Loading a cookie on page_load???
Hello, I'm trying to load a cookie when the page loads up, to display a persons name, something like "Welcome back Denna" (Where 'Denna' is the cookie value), now, I'm using the following code to save the cookie when someone places their name in a textbox:-
{code}
HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie.Value = tbxLogin.Text;
Response.Cookies.Add(myCookie);
{/code}
Now straight after that code, I load the cookie back up using the following code:-
{code}
HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
getMyCookie = Request.Cookies["MyTestCookie"];
if (getMyCookie.Value != "")
{
lblName.Text = "Welcome " + getMyCookie.Value;
}
{/code}
Now this works great when the person clicks the'login' button, but I want to use the second code snippet above to load the cookie when the page loads up, I';ve tried putting it in the 'Page_Load' Function, but it cause's errors... The errors are pointing to this line:-
if (getMyCookie.Value != "")
Here is a full thingy of the error report it gives me:-
==========================================================
Server Error in '/index' Application.
--------------------------------------------------------------------------------
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 38:
Line 39: //Read the cookie information and display it.
Line 40: if (getMyCookie.Value != null)
Line 41: {
Line 42: lblName.Text = "Welcome " + getMyCookie.Value;
Source File: c:\inetpub\wwwroot\index\webform1.aspx.cs Line: 40
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
index.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\index\webform1.aspx.cs:40
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
=========================================================
Can anyone explain what that means, or give me some pointers on how to load a cookie on startup... I'm using Visual C#.NET 7 to build the page... oh, and I'm new to C#.NET, so go easy!!! ;)
Thanks in advance
-Denna