plz go through the following code and give solution to me..... Thanks a lot
public partial class Home : System.Web.UI.Page
{
HttpCookie cookie = new HttpCookie("userdetails");
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["userdetails"] != null)
{
txtUsername.Text = Request.Cookies["userdetails"]["username"];
txtPassword.Text = Request.Cookies["userdetails"]["Password"]; // it is not working
}
if (!IsPostBack)
{
lblViewersCount.Text = "You are the Viewer Number " + Application["ViewersCount"].ToString();
}
}
protected void button_click(object sender, ImageClickEventArgs e)
{
cookie.Values["username"] = txtUsername.Text;
cookie.Values["Password"] = txtPassword.Text; // password value is not assigning to the cookie so while retrieving it is giving null value
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
}
Loading
Anand NPosted Oct 17, 2012, 6:07 AM
Ritesh DiyawarPosted Oct 17, 2012, 3:27 AM
Just Checkout these sample and rectify your code, let me no if have any problem
in order to handle a cookie in your system you should aware of 2 thing:
1-how to create them
2- how to delete them
so,first in order to create a cookie you have 2 variable must be defined as they are the cookie name and the duration of the cookie before it expired
////////////SET THE COOKIE//////////////
Response.Cookies["test"].Value = UserName.Text ;
Response.Cookies["test"].Expires = DateTime.Now.AddYears(30);
where test is the cookie name
and the username.text is the textbox that will be used to define the cookies information
Response.Cookies["test"].Expires = DateTime.Now.AddYears(-30);
/////////check to see if cookie presented//////////
if (Request.Cookies["test"] == null)
TextBox2.Text = "No cookie found";
else
TextBox2 .Text = Request.Cookies["test"].Value;
/////////END check to see if cookie presented//////////