private void OnSubmit(object parameter) { try { _deviceObj.id = ID; // ideally this has to be read only if it is an Identity column _deviceObj.location = Location; _deviceObj.type = Type; _deviceObj.device_health = Device_Health; _deviceObj.last_used = Last_Used.Date; _deviceObj.price = Convert.ToDecimal(Price); _deviceObj.color = Color; using (var db = new DeviceInfoManagerEntities()) { var entity = db.tblDeviceDetails.Find(_deviceObj.id); if (entity == null) { return; } db.Entry(entity).CurrentValues.SetValues(_deviceObj); db.SaveChanges(); } MessageBox.Show("Device details saved."); _mainVMCallback.LoadDeviceDetails(); } catch (Exception ex) { MessageBox.Show("Unexpected error occured. \n" + ex.InnerException); } }
private void OnDeleteDeviceDetails(object parameter) { try { if (MessageBox.Show("Are you sure you really want to delete this entry?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { using (var db = new DeviceInfoManagerEntities()) { var entity = db.tblDeviceDetails.Find(SelectedDevice.id); if (entity == null) { return; } db.tblDeviceDetails.Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } LoadDeviceDetails(); } } catch (Exception ex) { MessageBox.Show("Unexpected error occured during Delete device details. \n" + ex.Message); } }
private void OnSubmit(object parameter) { try { var deviceObj = new tblDeviceDetails(); deviceObj.id = ID; deviceObj.location = Location; deviceObj.type = Type; deviceObj.device_health = Device_Health; deviceObj.last_used = Last_Used.Date; deviceObj.price = Convert.ToDecimal(Price); deviceObj.color = Color; using (var db = new DeviceInfoManagerEntities()) { db.tblDeviceDetails.Add(deviceObj); db.SaveChanges(); } MessageBox.Show("Device details successfully added!"); _mainVMCallback.LoadDeviceDetails(); } catch (Exception ex) { MessageBox.Show("Unexpected error occured. \n" + ex.InnerException); } }
public void LoadDeviceDetails() { //Fetch device details from DB using (var ctx = new DeviceInfoManagerEntities()) { var q = (from a in ctx.tblDeviceDetails select a).ToList(); this.Devices = new ObservableCollection <tblDeviceDetails>(q); } }