public bool InsertOrUpdate(string connectString, Q_ServiceLimit model) { using (db = new QMSSystemEntities(connectString)) { if (CheckExists(model) == null) { if (model.Id == 0) { model.CurrentDay = DateTime.Now.ToString("dd/MM/yyyy"); db.Q_ServiceLimit.Add(model); } else { var obj = db.Q_ServiceLimit.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id); if (obj != null) { obj.Quantity = model.Quantity; obj.ServiceId = model.ServiceId; db.SaveChanges(); } else { return(false); } } db.SaveChanges(); return(true); } else { return(false); } } }
private void gridViewService_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { try { int Id = 0; int.TryParse(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "Id").ToString(), out Id); if (Id == 0 && string.IsNullOrEmpty(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "Quantity").ToString())) { goto End; } else if (Id == 0 && string.IsNullOrEmpty(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "ServiceId").ToString())) { goto End; } if (Id != 0 && string.IsNullOrEmpty(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "ServiceId").ToString())) { MessageBox.Show("Vui lòng chọn dịch vụ", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Id != 0 && string.IsNullOrEmpty(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "Quantity").ToString())) { MessageBox.Show("Vui lòng nhập số phiếu giới hạn cho phép.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var obj = new Q_ServiceLimit(); obj.Id = Id; obj.ServiceId = int.Parse(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "ServiceId").ToString()); obj.UserId = userId; obj.Quantity = int.Parse(gridViewService.GetRowCellValue(gridViewService.FocusedRowHandle, "Quantity").ToString()); if (obj.Id == 0) { bool result = BLLServiceLimit.Instance.InsertOrUpdate(connect, obj); if (!result) { MessageBox.Show("Dịch vụ đã tồn tại thời gian cấp phiếu này. Xin chọn lại", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } else { bool result = BLLServiceLimit.Instance.InsertOrUpdate(connect, obj); if (!result) { MessageBox.Show("Dịch vụ đã tồn tại thời gian cấp phiếu này. Xin chọn lại", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); goto End; } } LoadGrid(); } } catch (Exception ex) { } End : { } }
private Q_ServiceLimit CheckExists(Q_ServiceLimit model) { return(db.Q_ServiceLimit.FirstOrDefault(x => !x.IsDeleted && x.Id != model.Id && x.UserId == model.UserId && x.ServiceId == model.ServiceId)); }