public static PhotoContestRank[] Load(string username, int? contestId, int? entryId, int? value)
        {
            List<PhotoContestRank> ranks = new List<PhotoContestRank>();
            
            //using (var conn = Config.DB.Open())
            {
                using (var reader = SqlHelper.GetDB().ExecuteReader("LoadPhotoContestRanks", username,
                                                                    contestId, entryId, value))
                {
                    while (reader.Read())
                    {
                        PhotoContestRank rank = new PhotoContestRank();

                        rank.username = (string) reader["Username"];
                        rank.contestId = (int) reader["ContestId"];
                        rank.entryId = (int) reader["EntryId"];
                        rank.value = (int) reader["Value"];

                        ranks.Add(rank);
                    }
                    reader.Close();
                }
            }

            return ranks.ToArray();
        }
        private void rankEntries(PhotoContestEntry pick, PhotoContestEntry nonpick)
        {
            Session["LastShowedEntries_" + contestId] = null;

            #region Check for duplicate ranking (usually caused by refreshing)

            string lastRanked = leftEntry.Id + "_" + rightEntry.Id;
            if ((string) Session["PhotoContestPage_lastRankEntries"] == lastRanked) return;
            Session["PhotoContestPage_lastRankEntries"] = lastRanked;

            #endregion

            #region Show last ranked

            divNoRankedMessage.Visible = false;
            divLastRankedMessage.Visible = true;

            imgLastLeft.ImageUrl = ImageHandler.CreateImageUrl(pick.PhotoId, 50, 50, false, false, false);
            linkLastLeft.InnerHtml = pick.Username;
            linkLastLeft.HRef = UrlRewrite.CreateShowUserUrl(pick.Username);
            linkLastLeft.Target = "_new";

            imgLastRight.ImageUrl = ImageHandler.CreateImageUrl(nonpick.PhotoId, 50, 50, false, false, false);
            linkLastRight.InnerHtml = nonpick.Username;
            linkLastRight.HRef = UrlRewrite.CreateShowUserUrl(nonpick.Username);
            linkLastRight.Target = "_new";

            lblVotersAgree.Text = String.Format(Lang.Trans("{0}% of voters agree"), Convert.ToInt32(
                                                                                       PhotoContestVotes.FetchPercentage
                                                                                           (pick.Id, nonpick.Id).
                                                                                           GetValueOrDefault(100)));

            #endregion

            #region Save vote

            try
            {
                PhotoContestVotes.SaveVote(CurrentUserSession.Username, pick.Id, nonpick.Id);
            }
            catch (Exception err)
            {
                Global.Logger.LogError("rankEntries", err);
            }

            #endregion

            #region Update ranking

            PhotoContestRank[] ranks = PhotoContestRank.Load(CurrentUserSession.Username, contestId);

            if (ranks == null || ranks.Length == 0)
            {
                PhotoContestRank rank1 = new PhotoContestRank(CurrentUserSession.Username, contestId,
                                                              pick.Id, 1);
                rank1.Save();
                PhotoContestRank rank2 = new PhotoContestRank(CurrentUserSession.Username, contestId,
                                                              nonpick.Id, 2);
                rank2.Save();
                currentEntry = null;
                currentEntryMinRank = null;
                currentEntryMaxRank = null;
                loadPersonalFavs();
            }
            else if (currentEntry == null)
            {
                currentEntry = pick;
                currentEntryMinRank = null;
                currentEntryMaxRank = null;
            }
            else
            {
                if (currentEntry.Id == pick.Id)
                {
                    // The user picked the newly introduced photo
                    foreach (PhotoContestRank rank in ranks)
                    {
                        // Find the rank of the other (already ranked) photo
                        if (rank.EntryId == nonpick.Id)
                        {
                            // Check if the picked photo reached first rank
                            if (rank.Value == 1)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, pick.Id, 1);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                                // Check if the picked photo found its rank
                            else if (rank.Value - currentEntryMinRank == 1)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, pick.Id, rank.Value);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                            else
                            {
                                // Limit the maximum rank
                                currentEntryMaxRank = rank.Value;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    // The user picked one if his previously ranked photos
                    foreach (PhotoContestRank rank in ranks)
                    {
                        // Find the rank of the picked (already ranked) photo
                        if (rank.EntryId == pick.Id)
                        {
                            // Check if the non picked photo is going out of the chart
                            if (rank.Value == Config.Ratings.FavoriteEntriesCount)
                            {
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                            }
                                // Check if the picked photo found its rank
                            else if (currentEntryMaxRank - rank.Value == 1 || rank.Value == ranks.Length)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, nonpick.Id, rank.Value + 1);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                            else
                            {
                                // Limit the minimum rank
                                currentEntryMinRank = rank.Value;
                            }
                            break;
                        }
                    }
                }
            }

            #endregion
        }