I want to store the login name in the login page in a cookie (when the user clicks the remember id checkbox )so that the next time he opens the login page his name should appear in the login name box.
Can anyone give me the code for this?
but when i try to retrieve the value stored in the cookie on the page load function in the user name text box
by the code : txtusername.Text = Request.Cookies("UserName").Value.ToString
it's throwing error.
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 56: 'If Not (Request.Cookies("UserName") Is Nothing) Then
Line 57: 'txtusername.Text = Response.Cookies.Get("USER_NAME").Value
Line 58: txtusername.Text = Request.Cookies("UserName").Value.ToString
Line 59: 'If (Request.Cookies("Name") Is Nothing) Then
Line 60: ' txtusername.Text = Request.Cookies("Name").Value
I am getting this error when i am trying to retrive the value of the cookie:
Server Error in '/ParamountCRS' 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 52: Dim Tempcookie As HttpCookie
Line 53: Tempcookie = Response.Cookies.Get("ckusername")
Line 54: txtusername.Text = Tempcookie.Values("UserName").ToString
Line 55: 'Response.Write(ckusername.Name)
Line 56: 'Request.Cookies.Get("sonika")
Make sure the cookie exists before you try to retrieve. Your code is failing because the cookie does not exist in the Page_Load of the login page.. Try doing the following
sonika
Member
35 Points
7 Posts
How to store user name in cookie ?
Oct 20, 2005 04:04 PM|LINK
Can anyone give me the code for this?
response.coo...
Member
350 Points
80 Posts
Re: How to store user name in cookie ?
Oct 20, 2005 08:39 PM|LINK
if(mycheckbox.Checked)
{
Response.Cookies["cookiename"].Value = this.mytextbox.Text;
Response.Cookies["cookiename"].Expires = DateTime.Now.AddDays(2);
}
Then later if you want to retreive the cookie value:
string mystring = Request.Cookies["cookiename"].Value.ToString();
Hope this helps,
LW
Kumar Reddi
Star
14340 Points
2619 Posts
Re: How to store user name in cookie ?
Oct 20, 2005 09:10 PM|LINK
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpResponseClassCookiesTopic.asp
sonika
Member
35 Points
7 Posts
Re: How to store user name in cookie ?
Oct 20, 2005 09:28 PM|LINK
ckusername =
New HttpCookie("UserName", txtusername.Text)Response.Cookies.Add(ckusername)
Response.Cookies("ckusername").Expires = DateTime.Now.AddDays(20)
Response.Write(Request.Cookies("UserName").Value)This returns the user name in the response.write
but when i try to retrieve the value stored in the cookie on the page load function in the user name text box by the code :
txtusername.Text = Request.Cookies("UserName").Value.ToString
it's throwing error.
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:
sonika
Member
35 Points
7 Posts
Re: How to store user name in cookie ?
Oct 20, 2005 10:32 PM|LINK
I am getting this error when i am trying to retrive the value of the cookie:
Server Error in '/ParamountCRS' 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:
Kumar Reddi
Star
14340 Points
2619 Posts
Re: How to store user name in cookie ?
Oct 21, 2005 01:17 AM|LINK
Make sure the cookie exists before you try to retrieve. Your code is failing because the cookie does not exist in the Page_Load of the login page.. Try doing the following
Page_Load(..)
{
if(Request.Cookies["UserName"] != null)
{
txtUserName.Text = Request.Cookies["UserName"].Value;
}
else
{
HttpCookie MyCookie = new HttpCookie("UserName", txtusername.Text);
DateTime now = DateTime.Now;
MyCookie.Value = now.ToString();
MyCookie.Expires = now.AddHours(1);
Response.Cookies.Add(MyCookie);
}
}
This would set the cookie the first time the user logsin.. So from next time on the cookie is read from the browser
P.S: The above code is untested and c# version..