7 Replies - 1485 Views - Last Post: 07 December 2013 - 06:33 PM Rate Topic: -----

#1 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Registration counter with Cookies

Posted 07 December 2013 - 03:55 PM

I was wondering how I might go about having a registration form with a countdown of number of times you have left before you have to register.

So if a user doesnt enter their information the first time they would have 4 times left to register. I know that I need to use cookies but how would i do a counter cookie?

Can someone point me in the right direction here.


Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Registration counter with Cookies

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3352
  • View blog
  • Posts: 11,343
  • Joined: 12-December 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 04:25 PM

Quote

I know that I need to use cookies but how would i do a counter cookie?

A cookie value cannot be incremented or edited. You have to:

  • Attempt to read the cookie-value.
  • If it doesn't exist, create it and store, say, 1.
  • If it does exist, convert the value to a number.
  • Add one to this number (if appropriate).
  • Re-create the cookie, using the same name but with the new value.


ASP.NET Cookies Overview

int counter;
if (Request.Cookies["counter"] == null)
    counter = 0;
else
{
    counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;

Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);

Was This Post Helpful? 0
  • +
  • -

#3 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 04:55 PM

So I got the code working...Now what I need to do is get the name and email from the cookie on another form how would i go about that?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

            HttpCookie cookie = Request.Cookies["Register"];
            if (cookie == null)
            {
                cookie = new HttpCookie("Register");
                cookie.Value = "5";
                cookie.Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                int daysToRegister = int.Parse(cookie.Value);
                cookie.Value = (daysToRegister - 1).ToString();
                
                cookie.Expires = DateTime.Now.AddDays(30);
                               
              
            }


                    int aTime = int.Parse(cookie.Value);
                    Response.Cookies.Add(cookie);
                    lblDaysToRegister.Text = "Times Left To Register: " + cookie.Value;
                    Response.Redirect("Registered.aspx");
            if (aTime < 0)
                {
                registerForm.Visible = false;
                    lblDaysToRegister.Text = "Sorry you have not registered in the alotted time.";
                }
        
                    
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3352
  • View blog
  • Posts: 11,343
  • Joined: 12-December 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 05:08 PM

Did you try

Request.Cookies["name"];    // or email

Cookies are not stored with a Form. They are stored in the browser, based on the current domain, or sub-domain.

You should read the link I provided to understand cookies better.
Was This Post Helpful? 0
  • +
  • -

#5 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 06:04 PM

Maybe you can help me see where I am going wrong. It is giving me a null reference exception.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Registered : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String name = Response.Cookies["Name"].Value;
   
        
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

            HttpCookie cookie = Request.Cookies["Register"];
            if (cookie == null)
            {
                cookie["Name"] = txtName.Text;
                cookie["Email"] = txtEmail.Text;
                cookie = new HttpCookie("Register");
                cookie.Value = "5";
                cookie.Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                int daysToRegister = int.Parse(cookie.Value);
                cookie.Value = (daysToRegister - 1).ToString();

                cookie.Expires = DateTime.Now.AddDays(30); 
                               
              
            }


                    int aTime = int.Parse(cookie.Value);
                    Response.Cookies.Add(cookie);
                    lblDaysToRegister.Text = "Times Left To Register: " + cookie.Value;
                    Response.Redirect("Registered.aspx");
            if (aTime < 0)
                {
                registerForm.Visible = false;
                    lblDaysToRegister.Text = "Sorry you have not registered in the alotted time.";
                }
        
                    
    }
}



Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3352
  • View blog
  • Posts: 11,343
  • Joined: 12-December 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 06:24 PM

Quote

It is giving me a null reference exception.

Where? What line in your posted code?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3352
  • View blog
  • Posts: 11,343
  • Joined: 12-December 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 06:32 PM

It's probably this line:

cookie["Name"] = txtName.Text;

An HttpCookie is a singular object (although it can have subkeys) but you are treating it like an array.

Did you study the page I linked to?
Was This Post Helpful? 0
  • +
  • -

#8 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Re: Registration counter with Cookies

Posted 07 December 2013 - 06:33 PM

Now it is working but it isnt taking the information and redirecting to the second page it redirects to that page without any information. It initially loads the registered page. If i take that line of code out it will take my information but automatically say 4 days left to register and not send the information to the registered page.

does that make sense?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

            HttpCookie cookie = Request.Cookies["Register"];
            if (cookie == null)
            {
               
                cookie = new HttpCookie("Register");
                Response.Cookies["Register"]["Name"] = txtName.Text;
                Response.Cookies["Register"]["Email"] = txtEmail.Text;
                cookie.Value = "5";
                cookie.Expires = DateTime.Now.AddDays(1);
            }
            else
            {
                int daysToRegister = int.Parse(cookie.Value);
                cookie.Value = (daysToRegister - 1).ToString();

                cookie.Expires = DateTime.Now.AddDays(1); 
                               
              
            }
                    int aTime = int.Parse(cookie.Value);
                    Response.Cookies.Add(cookie);
                    lblDaysToRegister.Text = "Times Left To Register: " + cookie.Value;
                    Response.Redirect("Registered.aspx");
            if (aTime <= 0)
                {
                registerForm.Visible = false;
                    lblDaysToRegister.Text = "Sorry you have not registered in the alotted time.";
                }
        
                    
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Registered : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String name = Request.Cookies["Register"]["Name"];
        lblRegisteredName.Text = name;
        
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1