Applying Logic to Overwrite Cookies consecutively

Hi All,

I am in the midst of creating a 'Last Viewed' cart function which in a nutshell records that last 2 products the user viewed.

I am creating this function using 2 cookies called - productID1 and ProductID2 which stores the database IDs of the product.

My problem is implementing the logic to create the cookies when on the product page...

I had the following IF statement in place initially (which I know is wrong) but to find that once 2 products have been viewed and both cookies have been created then the 3rd product the user looks at this should overwrite productID1 and the 4th product viewed should overwrite ProductID2 and so on and so on!

Please could anyone suggest how I could change the logic so that once both cookies are created, my code overwrites the first cookie and then second and so on...

Many thanks,

Rit

if (!Page.IsPostBack)
        {
            if (Request.Cookies["ProductID1"] == null)
            {
                // create cookie
            }
            else if (Request.Cookies["ProductID2"] == null)
            {
                // create cookie
            }
        }

Open in new window

LVL 1
rito1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AdamSenior DeveloperCommented:
You just need to detect if both cookies exist and if so, update the first with the value of the second, and update the second with the new value.
This way, the first cookie is always the first product visited and the second always the most recent.

You can put the SetCookies function below in the !IsPostBack section as you have already done, but you do need to get the new ProductID first.

To improve it, you can also add checks to see if the latest ProductID is already saved in one of the cookies and ignore if it is
private void SetCookies(int ProductID)
{
    // If no Product Cookies exist, create the first one
    if (Request.Cookies["Product1"] == null)
    {
        Response.Cookies.Add(new HttpCookie("Product1", ProductID.ToString()));
    }
    // If the first exists and not the second, create the second one
    else if (Request.Cookies["Product2"] == null)
    {
        Response.Cookies.Add(new HttpCookie("Product2", ProductID.ToString()));
    }
    // Both cookies exist, so move the value of the second to the first, and update the second
    else
    {
        Response.Cookies.Set(new HttpCookie("Product1", Request.Cookies["Product2"].Value));
        Response.Cookies.Set(new HttpCookie("Product2", ProductID.ToString()));
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rito1Author Commented:
Cyber-spy, that is much appreciated indeed. Its spot on what I need!

Thanks,

Rit
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.