Beispiel #1
0
        /// <summary>
        /// Fetches Profile Choices from the DB for the specified question
        /// </summary>
        /// <param name="QuestionID">ID of the question</param>
        /// <returns>Array of ProfileChoice objects</returns>
        public static ProfileChoice[] FetchByQuestionID(int QuestionID)
        {
            string cacheKey = String.Format("ProfileChoice_FetchByQuestionID_{0}", QuestionID);
            if (HttpContext.Current != null && HttpContext.Current.Cache[cacheKey] != null)
            {
                return HttpContext.Current.Cache[cacheKey] as ProfileChoice[];
            }

            using (SqlConnection conn = Config.DB.Open())
            {
                SqlDataReader reader =
                    SqlHelper.ExecuteReader(conn, "FetchProfileChoiceByQuestion", QuestionID);

                var lChoices = new List<ProfileChoice>();

                while (reader.Read())
                {
                    var choice = new ProfileChoice
                                     {
                                         id = ((int) reader["ID"]),
                                         questionId = QuestionID,
                                         _value = ((string) reader["Value"])
                                     };

                    lChoices.Add(choice);
                }

                ProfileChoice[] result = lChoices.ToArray();
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Cache.Insert(cacheKey, result, null, Cache.NoAbsoluteExpiration,
                                                     TimeSpan.FromHours(1), CacheItemPriority.NotRemovable, null);
                }

                if (result.Length > 0)
                {
                    return result;
                }
                return null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Fetches Profile Choice from the DB
        /// </summary>
        /// <param name="Id">Id of the choice</param>
        /// <returns>ProfileChoice object</returns>
        /// <exception cref="NotFoundException">No choice was found with the requested Id</exception>
        public static ProfileChoice Fetch(int Id)
        {
            string cacheKey = String.Format("ProfileChoice_Fetch_{0}", Id);
            if (HttpContext.Current != null && HttpContext.Current.Cache[cacheKey] != null)
            {
                return HttpContext.Current.Cache[cacheKey] as ProfileChoice;
            }

            using (SqlConnection conn = Config.DB.Open())
            {
                SqlDataReader reader =
                    SqlHelper.ExecuteReader(conn, "FetchProfileChoice", Id);

                var choice = new ProfileChoice {id = Id};

                if (reader.Read())
                {
                    choice.questionId = (int) reader["QuestionID"];
                    choice._value = (string) reader["Value"];
                }
                else
                {
                    throw new NotFoundException
                        (Lang.Trans("The requested choice does not exist!"));
                }

                if (HttpContext.Current != null)
                {
                    //Global.AddCacheItem("ProfileChoices", cacheKey, choice);
                    HttpContext.Current.Cache.Insert(cacheKey, choice, null, Cache.NoAbsoluteExpiration,TimeSpan.FromHours(1), CacheItemPriority.NotRemovable, null);
                }

                return choice;
            }
        }