/// <summary>
 /// Kiểm tra và thêm mới PollCategory
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của PollCategory Mới Thêm Vào</returns>
 public static Int32 Add(PollCategoryEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return PollCategoryDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa PollCategory
 /// </summary>
 /// <param name="entity">PollCategoryEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(PollCategoryEntity entity)
 {
     checkExist(entity.iPollCategoryID);
     checkLogic(entity);
     checkDuplicate(entity, true);
     checkFK(entity);
     return PollCategoryDAL.Edit(entity);
 }
 protected void btnOK_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         try
         {
             PollEntity oPoll = new PollEntity();
             oPoll.sQuestion = txtQuestion.Text;
             oPoll.tDate = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", null);
             oPoll.iOrder = Convert.ToByte(txtOrder.Text);
             oPoll.bHomepage = chkHomepage.Checked;
             if (Session["NewsID"] != null)
             {
                 int newsID;
                 bool result = Int32.TryParse(Session["NewsID"].ToString(), out newsID);
                 if (result) oPoll.iNewsID = newsID;
             }
             if (btnOK.CommandName == "Edit")
             {
                 oPoll.iPollID = Convert.ToInt32(btnOK.CommandArgument);
                 PollBRL.Edit(oPoll);
                 //Cập nhật trong PollCategory
                 foreach (ListItem item in lstbNhomtin.Items)
                 {
                     try
                     {
                         int categoryID = Convert.ToInt32(item.Value);
                         if (!item.Selected)
                         {
                             PollCategoryBRL.RemoveByiCategoryID(categoryID);
                         }
                         else
                         {
                             PollCategoryEntity oPollCat = new PollCategoryEntity();
                             oPollCat.iPollID = oPoll.iPollID;
                             oPollCat.iCategoryID = categoryID;
                             PollCategoryBRL.Add(oPollCat);
                         }
                     }
                     catch { }
                 }
             }
             else
             {
                 int pollID = PollBRL.Add(oPoll);
                 //Thêm bản ghi vào tblAdvCategory
                 if (pollID > 0)
                 {
                     foreach (ListItem item in lstbNhomtin.Items)
                     {
                         if (item.Selected)
                         {
                             PollCategoryEntity oPollCat = new PollCategoryEntity();
                             oPollCat.iPollID = pollID;
                             oPollCat.iCategoryID = Convert.ToInt32(item.Value);
                             PollCategoryBRL.Add(oPollCat);
                         }
                     }
                 }
             }
             lblThongbao.Text = "Cập nhật thành công";
             //Nạp lại dữ liệu
             Response.Redirect(Request.Url.ToString());
         }
         catch (Exception ex)
         {
             Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=PollManager';</script>");
         }
     }
 }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">PollCategoryEntity: entity</param>
 private static void checkLogic(PollCategoryEntity entity)
 {
     if (entity.iPollID < 0)
         throw new Exception(EX_IPOLLID_INVALID);
     if (entity.iCategoryID < 0)
         throw new Exception(EX_ICATEGORYID_INVALID);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">PollCategoryEntity:entity</param>
 private static void checkFK(PollCategoryEntity entity)
 {
     PollEntity oPoll = PollDAL.GetOne(entity.iPollID);
     if (oPoll==null)
     {
         throw new Exception("Không tìm thấy bình chọn này");
     }
     CategoryEntity oCategory = CategoryDAL.GetOne(entity.iCategoryID);
     if (oCategory==null)
     {
         throw new Exception("Không tìm thấy nhóm tin này");
     }
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">PollCategoryEntity: PollCategoryEntity</param>
 private static void checkDuplicate(PollCategoryEntity entity,bool checkPK)
 {
     List<PollCategoryEntity> list = PollCategoryDAL.GetAll();
     if (list.Exists(
         delegate(PollCategoryEntity oldEntity)
         {
             bool result = oldEntity.iCategoryID == entity.iCategoryID && oldEntity.iPollID == entity.iPollID;
             if(checkPK)
                 result=result && oldEntity.iPollCategoryID != entity.iPollCategoryID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_EXISTED);
     }
 }