private bool CheckExists(Q_Language model) { Q_Language obj = null; if (!string.IsNullOrEmpty(model.Name)) { obj = db.Q_Language.FirstOrDefault(x => !x.IsDeleted && x.Id != model.Id && x.Name.Trim().ToUpper().Equals(model.Name.Trim().ToUpper())); } return(obj != null ? true : false); }
public int Insert(string connectString, Q_Language obj) { using (db = new QMSSystemEntities(connectString)){ if (!CheckExists(obj)) { db.Q_Language.Add(obj); db.SaveChanges(); } return(obj.Id); } }
private void gridViewLanguage_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { try { int Id = 0; int.TryParse(gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Id").ToString(), out Id); if (Id == 0 && string.IsNullOrEmpty(gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Name").ToString())) { goto End; } if (Id != 0 && string.IsNullOrEmpty(gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Name").ToString())) { MessageBox.Show("Vui lòng nhập tên ngôn ngữ.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var obj = new Q_Language(); obj.Id = Id; obj.Name = gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Name").ToString(); obj.Note = gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Note") != null?gridViewLanguage.GetRowCellValue(gridViewLanguage.FocusedRowHandle, "Note").ToString() : ""; if (obj.Id == 0) { int result = BLLLanguage.Instance.Insert(connect, obj); if (result == 0) { MessageBox.Show("Tên ngôn ngữ này đã tồn tại. Xin nhập lại.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } else { bool result = BLLLanguage.Instance.Update(connect, obj); if (result == false) { MessageBox.Show("Tên ngôn ngữ này đã tồn tại. Xin nhập lại.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } GetGridLanguage(); } } catch (Exception ex) { } End: { } }
public bool Update(string connectString, Q_Language model) { using (db = new QMSSystemEntities(connectString)){ var obj = db.Q_Language.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id); if (obj != null) { if (!CheckExists(model)) { obj.Name = model.Name; obj.Note = model.Note; db.SaveChanges(); return(true); } } return(false); } }