/// <summary> /// Kiểm tra và thêm mới ChungchiChuyengia /// </summary> /// <param name="entity">Entity</param> /// <returns>Int32: ID của ChungchiChuyengia Mới Thêm Vào</returns> public static Int32 Add(ChungchiChuyengiaEntity entity) { checkLogic(entity); checkDuplicate(entity, false); checkFK(entity); return ChungchiChuyengiaDAL.Add(entity); }
/// <summary> /// Kiểm tra và chỉnh sửa ChungchiChuyengia /// </summary> /// <param name="entity">ChungchiChuyengiaEntity</param> /// <returns>bool:kết quả thực hiện</returns> public static bool Edit(ChungchiChuyengiaEntity entity) { checkExist(entity.PK_iChungchiChuyengiaID); checkLogic(entity); checkDuplicate(entity, true); checkFK(entity); return ChungchiChuyengiaDAL.Edit(entity); }
private bool luuChungchi(int PK_iChuyengiaID) { bool bThanhcong = true; try { List<ChungchiChuyengiaEntity> lstChungchichuyengia = ChungchiChuyengiaBRL.GetByFK_iChuyengiaID(PK_iChuyengiaID); GiaytonopkemhosoEntity oGiaytoNopkem = null; foreach (ListItem chk in cblGiaychungnhan.Items) { ChungchiChuyengiaEntity oChungchichuyengia = null; oChungchichuyengia = lstChungchichuyengia.Find( delegate(ChungchiChuyengiaEntity oChungchichuyengiadaco) { return oChungchichuyengiadaco.FK_iChungchiID.ToString().Equals(chk.Value); } ); if (oGiaytoNopkem == null) { if (chk.Selected) { ChungchiChuyengiaEntity oChungchichuyengiaMoi = new ChungchiChuyengiaEntity(); oChungchichuyengiaMoi.FK_iChungchiID = int.Parse(chk.Value); oChungchichuyengiaMoi.FK_iChuyengiaID = PK_iChuyengiaID; ChungchiChuyengiaBRL.Add(oChungchichuyengiaMoi); } } else { if (!chk.Selected) { ChungchiChuyengiaBRL.Remove(oChungchichuyengia.PK_iChungchiChuyengiaID); } lstChungchichuyengia.Remove(oChungchichuyengia); //Loại bỏ phần tử đã tìm thấy để tối ưu } } lstChungchichuyengia = null; napDanhgiaychungnhan(PK_iChuyengiaID); lblThongbao.Text = "Lưu thành công!"; } catch (Exception ex) { } return bThanhcong; }
protected void btnOK_Click(object sender, EventArgs e) { //Page.Validate("vgChuyengia"); //if (Page.IsValid) //{ try { Int32 PK_iChuyengiaID; ChuyengiaEntity oChuyengia = new ChuyengiaEntity(); oChuyengia.sHoten = txtHoten.Text; oChuyengia.FK_iTochucchungnhanID = int.Parse(ddlTochucchungnhan.SelectedValue); oChuyengia.iNamkinhnghiem = short.Parse(txtSonamkinhnghiem.Text); oChuyengia.sMaso = txtMaso.Text; oChuyengia.FK_iTrinhdoID = short.Parse(ddlTrinhdo.SelectedValue); oChuyengia.iNamsinh = Convert.ToInt16(txtNamsinh.Text); if (ddlDuyet.SelectedValue.Equals("1")) { oChuyengia.bDuyet = true; } else { oChuyengia.bDuyet = false; } byte[] bytImage = null; // xu ly anh if (fAnhthe.PostedFile != null) { if (fAnhthe.PostedFile.ContentLength > 0) { string strEx = "jpg|jpeg|bmp|png|gif"; string fileEx = fAnhthe.FileName.Substring(fAnhthe.FileName.LastIndexOf('.')).Remove(0, 1); string[] arrEx = strEx.Split('|'); bool valid = false; foreach (string ex in arrEx) { if (ex.Equals(fileEx, StringComparison.OrdinalIgnoreCase)) valid = true; } if (valid) { HttpPostedFile objHttpPostedFile = fAnhthe.PostedFile; int intContentlength = objHttpPostedFile.ContentLength; bytImage = new Byte[intContentlength]; objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength); oChuyengia.imAnh = bytImage; } } } else { } if (btnOK.CommandName.ToUpper() == "EDIT") { PK_iChuyengiaID = Convert.ToInt32(btnOK.CommandArgument); ChuyengiaEntity oChuyengiaOld = ChuyengiaBRL.GetOne(PK_iChuyengiaID); oChuyengia.imAnh = oChuyengiaOld.imAnh; oChuyengia.iTrangthai = oChuyengiaOld.iTrangthai; oChuyengia.PK_iChuyengiaID = PK_iChuyengiaID; luuChungchi(PK_iChuyengiaID); ChuyengiaBRL.Edit(oChuyengia); lblThongbao.Text = "Cập nhật thành công"; } else if(btnOK.CommandName.ToUpper()=="ADDNEW") { PK_iChuyengiaID = ChuyengiaBRL.Add(oChuyengia); // Lưu chứng chỉ của chuyên gia foreach (ListItem chk in cblGiaychungnhan.Items) { if (chk.Selected == true) { int PK_iChungchiID = Convert.ToInt32(chk.Value); ChungchiChuyengiaEntity oChungchichuyengiaMoi = new ChungchiChuyengiaEntity(); oChungchichuyengiaMoi.FK_iChungchiID = int.Parse(chk.Value); oChungchichuyengiaMoi.FK_iChuyengiaID = PK_iChuyengiaID; ChungchiChuyengiaBRL.Add(oChungchichuyengiaMoi); } } lblThongbao.Text = "Thêm thành công"; } napGridView(); } catch (Exception ex) { //lblThongbao.Text = ex.Message; Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=Chuyengia';</script>"); } pnlEdit.Visible = false; //} }
/// <summary> /// Kiểm tra logic Entity /// </summary> /// <param name="entity">ChungchiChuyengiaEntity: entity</param> private static void checkLogic(ChungchiChuyengiaEntity entity) { if (entity.FK_iChuyengiaID < 0) throw new Exception(EX_FK_ICHUYENGIAID_INVALID); if (entity.FK_iChungchiID < 0) throw new Exception(EX_FK_ICHUNGCHIID_INVALID); }
/// <summary> /// Kiểm tra tồn tại khóa ngoại /// </summary> /// <param name="entity">ChungchiChuyengiaEntity:entity</param> private static void checkFK(ChungchiChuyengiaEntity entity) { ChuyengiaEntity oChuyengia = ChuyengiaDAL.GetOne(entity.FK_iChuyengiaID); if (oChuyengia==null) { throw new Exception("Không tìm thấy :FK_iChuyengiaID"); } ChungchiEntity oChungchi = ChungchiDAL.GetOne(entity.FK_iChungchiID); if (oChungchi==null) { throw new Exception("Không tìm thấy :FK_iChungchiID"); } }
/// <summary> /// Kiểm tra trùng lặp bản ghi /// </summary> /// <param name="entity">ChungchiChuyengiaEntity: ChungchiChuyengiaEntity</param> private static void checkDuplicate(ChungchiChuyengiaEntity entity,bool checkPK) { /* Example List<ChungchiChuyengiaEntity> list = ChungchiChuyengiaDAL.GetAll(); if (list.Exists( delegate(ChungchiChuyengiaEntity oldEntity) { bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase); if(checkPK) result=result && oldEntity.PK_iChungchiChuyengiaID != entity.PK_iChungchiChuyengiaID; return result; } )) { list.Clear(); throw new Exception(EX_FIELD_EXISTED); } */ }