[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Applying Logic to Overwrite Cookies consecutively

Posted on 2009-07-03
2
Medium Priority
?
543 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:rito1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Adam earned 2000 total points
ID: 24774321
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
 
LVL 1

Author Closing Comment

by:rito1
ID: 31599571
Cyber-spy, that is much appreciated indeed. Its spot on what I need!

Thanks,

Rit
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question