Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 26th, 2008, 04:18 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default Bug in the Polls Module (and the fix for it)

I recently discovered a logic bug in the Polls module. I searched around and, interestingly enough, did not find this discussed anywhere.

You can reproduce the bug by doing the following:
  • Log in as an admin, and go to Admin/ManagePolls.aspx.
  • Make a note of which is the Current poll (green checkmark in the polls Gridview). Click on its pencil as if you were going to edit it. Note that it is marked IsCurrent in the poll DetailsView. Click Cancel to cancel the edit.
  • Go back up to the polls Gridview, and click to edit any poll which is NOT the current poll. Go down to the poll DetailsView, click the IsCurrent checkbox to mark it as the Current poll, and then click Update. Note that the green checkmark in the poll Gridview changes to the poll you just updated.
  • Now, click to edit the poll that WAS the most recently current poll (the first poll you looked at). You will note that in the poll DetailsView, IsCurrent is checked, even though this is not the current poll!
  • If you have several polls, you can click around in the polls Gridview, making different polls the Current poll. As you continue to do this, you will notice that any poll that was ever made Current will still be erroneously marked IsCurrent in the poll DetailsView.
Now, why is this happening? After a couple of hours of head-scratching, I figured it out, and thought I’d share it with everybody.

First off, note that if you set enableCaching="false" in web.config, the bug goes away. So, obviously, it’s a problem with the caching. More precisely, as it turns out, it’s a problem with the purging of the cache.

Look at the UpdatePoll method in MB.TheBeerHouse.BLL.Polls.Poll. This is the method that fires when you update a poll in the poll Detailsview:

public static bool UpdatePoll(int id, string questionText, bool isCurrent)
    {
        PollDetails record = new PollDetails(id, DateTime.Now, "", questionText, isCurrent, false, DateTime.Now, 0);
        bool ret = SiteProvider.Polls.UpdatePoll(record);
        BizObject.PurgeCacheItems("Polls_Polls_true");
        BizObject.PurgeCacheItems("Polls_Poll_" + id.ToString());
        if (isCurrent)
            BizObject.PurgeCacheItems("Polls_Poll_Current");
        return ret;
    }

Note what is getting purged from the cache here. It purges the list of active polls, the poll that you’re updating, and if you are setting this as the Current poll, the ID of the current poll. You should however note that is does NOT purge the poll that was current BEFORE this update was performed -- in other words, the poll that you are now making “un-Current”, so to speak.

When you edit a poll, the poll DetailsView uses the GetPollByID method to retrieve the poll to edit -- looking in the Cache first, of course. But because the poll that was before marked IsCurrent is never purged, the data in the Cache is stale, and the poll DetailsView therefore displays the wrong information.

The bottom line is, if you are setting a poll as the Current poll in the update, it is not enough to merely purge the ID of the current poll. You need to purge the actual poll that you are making “un-Current” as well. Note the changes in the method below that fix this bug:

public static bool UpdatePoll(int id, string questionText, bool isCurrent)
    {
        PollDetails record = new PollDetails(id, DateTime.Now, "", questionText, isCurrent, false, DateTime.Now, 0);
        int currPollID = Poll.CurrentPollID; //added
        bool ret = SiteProvider.Polls.UpdatePoll(record);
        Poll.PurgeCacheItems("Polls_Polls_true");
        Poll.PurgeCacheItems("Polls_Poll_" + id.ToString());
        if (isCurrent)
        {
            Poll.PurgeCacheItems("Polls_Poll_Current");
            Poll.PurgeCacheItems("Polls_Poll_" + currPollID.ToString()); //added
        }
        return ret;
    }

There are two new lines added. The first one gets the current poll ID, BEFORE the actual update is performed. This is the ID of the poll that is becoming “un-Current”. You need to get this value before the call to actually update the database, otherwise you will get the ID of the newly Current poll, which is not what you want. The second new line purges the poll that is becoming un-Current.

This ensures that only fresh data remains in the cache, and that poll DetailsView will display the right information.

Hope this helps some folks out.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
alfinmass (September 1st, 2009)
 
Old September 1st, 2009, 03:34 PM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default

This would happen as well when you insert a poll that you make current there and then, wouldn't it? So we ought to fix it there too, right?

By the way, I was going to write about another thing that was bugging me about the current poll, so I guess I will just do it here since it is related. Note that if no poll is set to be current, the property CurrentPollID will return -1.

In your fix we could check whether the id is different to -1 before we purge the cache, but it won't make much of a difference because if the prefix doesn't exist then it doesn't do anything when purging.

Where it can make a difference, however, is in the static property CurrentPoll. If there is no current poll, we will be querying the database for a poll with id -1 which does not exist. So why query at all?

The code in the book is

Code:
public static Poll CurrentPoll
{
     get
     {
        return GetPollByID(CurrentPollID);
     }
}
The code I suggest is:
Code:
public static Poll CurrentPoll
{
	get
	{
		int pollID = CurrentPollID;
		if (CurrentPollID != -1)
			return GetPollByID(pollID);
		return null;
	}
}
Thanks again Lee!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug or no to bug learning C using VS.Net to compil tesh All Other Wrox Books 0 February 14th, 2007 01:06 PM
Polls.Config is denied. eugz BOOK: ASP.NET Website Programming Problem-Design-Solution 1 November 18th, 2006 08:07 AM
Polls.Config nabeelalkaff BOOK: ASP.NET Website Programming Problem-Design-Solution 4 September 11th, 2006 09:21 PM
Service Pack 2 Bug Fix kerati Dreamweaver (all versions) 0 March 23rd, 2005 01:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.