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