private bool CheckExists(Q_Action model) { Q_Action obj = null; if (!string.IsNullOrEmpty(model.Code)) { obj = db.Q_Action.FirstOrDefault(x => !x.IsDeleted && x.Id != model.Id && x.Code.Trim().ToUpper().Equals(model.Code.Trim().ToUpper())); } return(obj != null ? true : false); }
public int Insert(Q_Action obj) { using (db = new QMSSystemEntities()) { if (!CheckExists(obj)) { db.Q_Action.Add(obj); db.SaveChanges(); } return(obj.Id); } }
private void gridViewAction_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { try { int Id = 0; int.TryParse(gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Id").ToString(), out Id); if (Id == 0 && string.IsNullOrEmpty(gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Code").ToString())) { goto End; } if (Id != 0 && string.IsNullOrEmpty(gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Code").ToString())) { MessageBox.Show("Vui lòng nhập mã hành động.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var obj = new Q_Action(); obj.Id = Id; obj.Index = int.Parse(gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Index").ToString()); obj.Code = gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Code").ToString(); obj.Function = gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Function") != null?gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Function").ToString() : ""; obj.Note = gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Note") != null?gridViewAction.GetRowCellValue(gridViewAction.FocusedRowHandle, "Note").ToString() : ""; if (obj.Id == 0) { int result = BLLAction.Instance.Insert(obj); if (result == 0) { MessageBox.Show("Mã hành động đã tồn tại. Xin nhập mã khác", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } else { bool result = BLLAction.Instance.Update(obj); if (result == false) { MessageBox.Show("Mã hành động đã tồn tại. Xin nhập mã khác", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } GetGridAction(); } } catch (Exception ex) { } End: { } }
public bool Update(Q_Action model) { using (db = new QMSSystemEntities()) { var obj = db.Q_Action.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id); if (obj != null) { if (!CheckExists(model)) { obj.Code = model.Code; obj.Index = model.Index; obj.Function = model.Function; obj.Note = model.Note; db.SaveChanges(); return(true); } else { return(false); } } return(false); } }