/// <summary>
        /// Inserts Gallery into the Gallerys Table
        /// </summary>
        /// <param name="gallery">A new populated gallery.</param>
        /// <returns>Insert Count</returns>
        public int Insert(Gallery gallery)
        {
            SqlParameter[] parameters =
            {
                    _database.MakeParameter("@Name",SqlDbType.NVarChar,50,gallery.Name),
                    _database.MakeParameter("@Description",SqlDbType.NVarChar,1024,gallery.Description),
                    _database.MakeParameter("@UserId",SqlDbType.Int,4,gallery.UserId)
            };

            return _database.NonQuery("Gallery_Insert", parameters);
        }
        /// <summary>
        /// Inserts Gallery into the Gallerys Table
        /// </summary>
        /// <param name="gallery">A new populated gallery.</param>
        /// <returns>Insert Count</returns>
        public int Insert(Gallery gallery, string commandText)
        {
            DbParameter[] parameters =
            {
                    DbClient.MakeParameter("@Name",DbType.String,50,gallery.Name),
                    DbClient.MakeParameter("@Description",DbType.String,1024,gallery.Description),
                    DbClient.MakeParameter("@AlbumID",DbType.Int32,4,gallery.AlbumID),
                    DbClient.MakeParameter("@UserId",DbType.Int32,4,gallery.UserId)
            };

            return DbClient.NonQuery(commandText, parameters);
        }
        /// <summary>
        /// Populates the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        internal Gallery Populate(SqlDataReader reader)
        {
            Gallery gallery = new Gallery();

            using (reader)
            {
                while (reader.Read())
                {
                    gallery = GetItem(reader);
                }
            }

            return gallery;
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            galleryId = Convert.ToInt32(Request["gid"]);
            gallery = new GalleryLogic().RetrieveGalleryByGalleryId(galleryId);

            Page.Title = string.Format("Photos in {0} | {1}", gallery.Name, Page.Title);

            //Generate breadcrumbnavigation
            BreadCrumbs breadCrumbNavigation = GenerateBreadCrumbNavigation();
            BuildNavigation(breadCrumbNavigation);

            if(!IsPostBack)
            {
                BindData();
            }
        }
        /// <summary>
        /// Handles the Click event of the addGallery control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void addGallery_Click(object sender, EventArgs e)
        {
            Gallery gallery = new Gallery
                                           {
                                               Name = galleryName.Text.Trim(),

                                               UserId = CurrentUser.UserId,
                                               Description = description.Text.Trim()
                                           };

               new GalleryLogic().Insert(gallery);

            //display message
            message.Text = string.Format(AdminResources.SuccessfulGalleryAdd, gallery.Name);

            //Clears the textboxes
            WebControlUtilities.ClearTextFromControl<TextBox>(Controls);
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            int galleryId;

            if (int.TryParse(Request.QueryString["lgid"], out galleryId))
            {
                gallery = new GalleryLogic().RetrieveGalleryByGalleryId(galleryId);
                //GetRandomPhotoFromGallery();

                galleryDescription.Text = gallery.Description;
                galleryTitle.Text = gallery.Name;

                //Set the three random photos
                GetRandomPhotoFromGallery(firstImageLiteral, firstImagePanel);
                GetRandomPhotoFromGallery(secondImageLiteral, secondImagePanel);
                GetRandomPhotoFromGallery(thirdImageLiteral, thirdImagePanel);

                //Generate breadcrumbnavigation
                BreadCrumbs breadCrumbNavigation = GenerateBreadCrumbNavigation();
                BuildNavigation(breadCrumbNavigation);
            }
        }
        /// <summary>
        /// Updates the Gallery table by the primary key, if the Gallery is dirty then an update will occur
        /// </summary>
        /// <param name="gallery">a populated gallery</param>
        /// <returns>update count</returns>
        public int Update(Gallery gallery, string commandText)
        {
            int updateCount = 0;

            if(gallery.IsDirty())
            {
                DbParameter[] parameters =
                {
                    DbClient.MakeParameter("@GalleryID",DbType.Int32,4,gallery.GalleryID),
                    DbClient.MakeParameter("@Name",DbType.String,50,gallery.Name),
                    DbClient.MakeParameter("@Description",DbType.String,150,gallery.Description),
                    DbClient.MakeParameter("@AlbumID",DbType.Int32,4,gallery.AlbumID),
                    DbClient.MakeParameter("@UserId",DbType.Int32,4,gallery.UserId)

                };

                updateCount = DbClient.NonQuery(commandText, parameters);

            }

            return updateCount;
        }
        /// <summary>
        /// Get photo count by userID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private Gallery GetItemWithPhotoCountAndUserId(IDataRecord reader)
        {
            Gallery gallery = new Gallery
            {
                GalleryID = Convert.ToInt32(reader["GalleryID"]),
                Name = Convert.ToString(reader["Name"]),
                Description = Convert.ToString(reader["Description"]),
                AlbumID = Convert.ToInt32(reader["AlbumID"]),
                UserId = Convert.ToInt32(reader["UserId"]),
                PhotoCount = (reader["PhotoCount"] != DBNull.Value ? Convert.ToInt32(reader["PhotoCount"]) : 0)
            };

            return gallery;
        }
        /// <summary>
        /// Get the gallery by it's Id
        /// </summary>
        /// <param name="galleryId"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public Gallery RetrieveGalleryByGalleryId(int galleryId, string commandText)
        {
            DbParameter[] parameters =
            {
                DbClient.MakeParameter("@GalleryId",DbType.Int32,4, galleryId)
            };

            GalleryItem items = GetItem;
            DbDataReader reader = DbClient.Reader(commandText, parameters);
            List<Gallery> galleries = PopulateGallerys(reader, items);

            Gallery gallery = new Gallery();

            if(galleries.Count > 0)
            {
                //only one should ever be returned.
               gallery = galleries[0];
            }

            return gallery;
        }
        /// <summary>
        /// Populates the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        internal static Gallery Populate(SqlDataReader reader, GalleryItem item)
        {
            Gallery gallery = new Gallery();

            using (reader)
            {
                while (reader.Read())
                {
                    gallery = item(reader);
                }
            }

            return gallery;
        }
        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private Gallery GetItem(IDataRecord reader)
        {
            Gallery gallery = new Gallery
            {
                GalleryID = Convert.ToInt32(reader["GalleryID"]),
                Name = Convert.ToString(reader["Name"]),
                Description = Convert.ToString(reader["Description"]),
                AlbumID = Convert.ToInt32(reader["AlbumID"]),
                UserId = Convert.ToInt32(reader["UserId"])
            };

            return gallery;
        }
 /// <summary>
 /// Inserts the specified gallery.
 /// </summary>
 /// <param name="gallery">The gallery.</param>
 public void Insert(Gallery gallery)
 {
     _resource.Insert(gallery);
 }
        /// <summary>
        /// No photo count. Need to add some extra logic to deal with the db.Null
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static Gallery GetItemWithPhotoCount(SqlDataReader reader)
        {
            Gallery gallery = new Gallery
                                  {
                                      GalleryId = Convert.ToInt32(reader["GalleryID"]),
                                      Name = Convert.ToString(reader["Name"]),
                                      Description = Convert.ToString(reader["Description"]),
                                      PhotoCount = (reader["PhotoCount"] != DBNull.Value ? Convert.ToInt32(reader["PhotoCount"]) : 0)
                                  };

            return gallery;
        }
        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static Gallery GetItemUserId(SqlDataReader reader)
        {
            Gallery gallery = new Gallery
            {
                GalleryId = Convert.ToInt32(reader["GalleryID"]),
                Name = Convert.ToString(reader["Name"]),
                Description = Convert.ToString(reader["Description"]),
                UserId = Convert.ToInt32(reader["UserId"])
            };

            return gallery;
        }
 /// <summary>
 /// Updates the Gallery table by the primary key, if the Gallery is dirty then an update will occur
 /// </summary>
 /// <param name="gallery">a populated gallery</param>
 /// <returns>update count</returns>
 public int Update(Gallery gallery)
 {
     int updateCount = galleryDb.Update(gallery, "Gallery_Update");
     return updateCount;
 }
 /// <summary>
 /// Inserts Gallery into the Gallerys Table
 /// </summary>
 /// <param name="gallery">A new populated gallery.</param>
 /// <returns>Insert Count</returns>
 public int Insert(Gallery gallery)
 {
     return galleryDb.Insert(gallery, "Gallery_Insert");
 }
        /// <summary>
        /// Updates the Gallery table by the primary key, if the Gallery is dirty then an update will occur
        /// </summary>
        /// <param name="gallery">a populated gallery</param>
        /// <returns>update count</returns>
        public int Update(Gallery gallery)
        {
            SqlParameter[] parameters =
                {
                    _database.MakeParameter("@GalleryID",SqlDbType.Int,4,gallery.GalleryId),
                    _database.MakeParameter("@Name",SqlDbType.NVarChar,50,gallery.Name),
                    _database.MakeParameter("@Description",SqlDbType.NVarChar,150,gallery.Description),
                    _database.MakeParameter("@UserId",SqlDbType.Int,4,gallery.UserId)
                };

            int updateCount = _database.NonQuery("Gallery_Update", parameters);

            return updateCount;
        }