1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to store and retrieve data from cookies

Discussion in 'ASP' started by hanusoftware, Jul 23, 2007.

  1. hanusoftware

    hanusoftware New Member

    Joined:
    Jun 6, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    This example shows that how to store and retrieve data from cookies.
    Code:
    /////////////////////////////////////////////////////////////////////////
    /// <b>Function: Page_Load</b>
    ///
    /// \param	sender
    ///
    /// \param	e
    ///
    /// \return	private void 
    ///
    /// \remarks	Initialize the page here
    ///
    /////////////////////////////////////////////////////////////////////////
    private void Page_Load(object sender, System.EventArgs e)
    {
    	// Put user code to initialize the page here
    	if(!IsPostBack)
    	{
    		if(Request.Cookies["UserCook"] != null)
    		{
    			chkRemember.Checked = true;
    			HttpCookie cook = Request.Cookies["UserCook"];
    			txtUserName.Text = cook["UserName"].ToString();
    			
    		}
    	}
    }
    
    
    /////////////////////////////////////////////////////////////////////////
    /// <b>Function: btnSubmit_Click</b>
    ///
    /// \param	sender
    ///
    /// \param	e
    ///
    /// \return	private void 
    ///
    /// \remarks	Submit button handler
    ///
    /////////////////////////////////////////////////////////////////////////
    private void btnSubmit_Click(object sender, System.EventArgs e)
    {
    	try
    	{
    		if(chkRemember.Checked == true)
    		{
    			if(Request.Browser.Cookies == true)
    			{
    				HttpCookie loginCook = new HttpCookie("UserCook");
    				loginCook["UserName"] = txtUserName.Text.Trim();
    				loginCook.Expires = DateTime.MaxValue;
    				Response.Cookies.Add(loginCook);
    			}
    			else
    			{
    				lblErr.Text = "Please enable your cookies in Internet
    					option in Explorer.";
    			}
    		}
    		if(txtUserName.Text.Trim() == "developer" &&
    			txtPwd.Text.Trim() == "developer")
    		{
    			Session["UserId"] = txtUserName.Text.Trim();
    			Response.Redirect("Querystring.aspx?Usertype=Web Developer");
    		}
    		else if(txtUserName.Text.Trim()=="admin" && txtPwd.Text.Trim()== "admin")
    		{
    			Session["UserId"] = txtUserName.Text.Trim();
    			string str = Server.UrlEncode("Administrator+");
    			
    			Response.Redirect("Querystring.aspx?UserType="+str);
    		}
    		else
    		{
    			lblErr.Text = "Invalid user id and password.";
    		}
    	}
    	catch(Exception ex)
    	{
    		Response.Write(ex.Message);
    	}
    }
     
  2. narayan

    narayan New Member

    Joined:
    Apr 9, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Good basics
     
  3. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    Really I needed this information, Thanks for sharing.
     

Share This Page