private void dataGridViewActions_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { if (dataGridViewActions.SelectedRows.Count > 0) { Action actionToDelete = dataGridViewActions.SelectedRows[0].Tag as Action; if (MessageBox.Show( $"Do you want to delete this action?", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { db.Remove(actionToDelete); db.SaveChanges(); dataGridViewActions.ClearSelection(); } } } catch (Exception ex) { MessageBox.Show($"Error when deleting: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { e.Cancel = true; } } }
private bool UpdatePlant(Plant plant, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantNameValid(db, textBoxPlantName.Text, plant.Id, out message)) { Plant plantToUpdate = db.Plants.FirstOrDefault(p => p.Id == plant.Id); if (plantToUpdate != null) { plantToUpdate.Name = textBoxPlantName.Text; PlantType selectedPlantType = comboBoxPlantTypes.SelectedItem as PlantType; plantToUpdate.PlantTypeId = selectedPlantType.Id; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedPlant = plantToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Plant '{textBoxPlantName.Text}': {ex.Message}"; } return(result); }
private bool AddPlant(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantNameValid(db, textBoxPlantName.Text, 0, out message)) { Plant plantToAdd = new Plant(); plantToAdd.Name = textBoxPlantName.Text; PlantType selectedPlantType = comboBoxPlantTypes.SelectedItem as PlantType; plantToAdd.PlantTypeId = selectedPlantType.Id; var trackedObjects = db.ChangeTracker.Entries(); db.Plants.Add(plantToAdd); db.SaveChanges(); selectedPlant = plantToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Plant '{textBoxPlantName.Text}': {ex.Message}"; } return(result); }
private bool UpdateActionType(ActionType actionType, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsActionTypeNameValid(db, textBoxActionTypeName.Text, actionType.Id, out message)) { ActionType actionTypeToUpdate = db.ActionTypes.FirstOrDefault(p => p.Id == actionType.Id); if (actionTypeToUpdate != null) { actionTypeToUpdate.Name = textBoxActionTypeName.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedActionType = actionTypeToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Action Type '{textBoxActionTypeName.Text}': {ex.Message}"; } return(result); }
private bool AddActionType(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsActionTypeNameValid(db, textBoxActionTypeName.Text, 0, out message)) { ActionType actionTypeToAdd = new ActionType(); actionTypeToAdd.Name = textBoxActionTypeName.Text; var trackedObjects = db.ChangeTracker.Entries(); db.ActionTypes.Add(actionTypeToAdd); db.SaveChanges(); selectedActionType = actionTypeToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Action Type '{textBoxActionTypeName.Text}': {ex.Message}"; } return(result); }
private bool UpdateLocation(Location location, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsLocationNameValid(db, textBoxLocationName.Text, location.Id, out message)) { Location locationToUpdate = db.Locations.FirstOrDefault(p => p.Id == location.Id); if (locationToUpdate != null) { locationToUpdate.Name = textBoxLocationName.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedLocation = locationToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Location '{textBoxLocationName.Text}': {ex.Message}"; } return(result); }
private bool AddLocation(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsLocationNameValid(db, textBoxLocationName.Text, 0, out message)) { Location locationToAdd = new Location(); locationToAdd.Name = textBoxLocationName.Text; var trackedObjects = db.ChangeTracker.Entries(); db.Locations.Add(locationToAdd); db.SaveChanges(); selectedLocation = locationToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Location '{textBoxLocationName.Text}': {ex.Message}"; } return(result); }
private void buttonDelete_Click(object sender, EventArgs e) { Plant plant = listBoxPlants.SelectedItem as Plant; if (MessageBox.Show( $"Do you want to delete {plant.Name}? \n" + $"(All Actions for this Plant will also be deleted)", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { Plant plantToDelete = db.Plants.FirstOrDefault(p => p.Id == plant.Id); var trackedObjects = db.ChangeTracker.Entries(); db.Plants.Remove(plantToDelete); db.SaveChanges(); LoadData(); ClearSelected(); toolStripLabel.Text = $"'{plantToDelete.Name}' is deleted."; } } } catch (Exception ex) { MessageBox.Show($"Error deleting Plant '{plant.Name}': {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private bool UpdatePlantType(PlantType plantType, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantTypeNameValid(db, textBoxPlantTypeName.Text, plantType.Id, out message)) { PlantType plantTypeToUpdate = db.PlantTypes.FirstOrDefault(p => p.Id == plantType.Id); if (plantTypeToUpdate != null) { plantTypeToUpdate.Name = textBoxPlantTypeName.Text; plantTypeToUpdate.Perennial = radioButtonYes.Checked; plantTypeToUpdate.Soil = textBoxSoil.Text; plantTypeToUpdate.Nutrition = textBoxNutrition.Text; plantTypeToUpdate.Info = textBoxInfo.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedPlantType = plantTypeToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Plant Type '{textBoxPlantTypeName.Text}': {ex.Message}"; } return(result); }
private bool AddPlantType(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantTypeNameValid(db, textBoxPlantTypeName.Text, 0, out message)) { PlantType plantTypeToAdd = new PlantType(); plantTypeToAdd.Name = textBoxPlantTypeName.Text; plantTypeToAdd.Perennial = radioButtonYes.Checked; plantTypeToAdd.Soil = textBoxSoil.Text; plantTypeToAdd.Nutrition = textBoxNutrition.Text; plantTypeToAdd.Info = textBoxInfo.Text; var trackedObjects = db.ChangeTracker.Entries(); db.PlantTypes.Add(plantTypeToAdd); db.SaveChanges(); selectedPlantType = plantTypeToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Plant Type '{textBoxPlantTypeName.Text}': {ex.Message}"; } return(result); }
private void dataGridViewActions_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } Action actionToUpdate; if (e.RowIndex == dataGridViewActions.NewRowIndex) { actionToUpdate = new Action(); } else { actionToUpdate = dataGridViewActions.Rows[e.RowIndex].Tag as Action; } var cell = dataGridViewActions.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (actionToUpdate.Id == 0) { if (dataGridViewActions.Rows[e.RowIndex].Cells["Location"].Value == null || dataGridViewActions.Rows[e.RowIndex].Cells["Plant"].Value == null || dataGridViewActions.Rows[e.RowIndex].Cells["ActionType"].Value == null) { return; } } if (!NeedsUpdating(actionToUpdate, cell)) { return; } try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (actionToUpdate.Id != 0) { actionToUpdate = db.Actions.FirstOrDefault(a => a.Id == actionToUpdate.Id); if (cell.OwningColumn.Name == "ActionType") { actionToUpdate.ActionType = cell.Value as ActionType; } if (cell.OwningColumn.Name == "ActionDate") { actionToUpdate.ActionDate = DateTime.Parse(cell.Value.ToString()); } if (cell.OwningColumn.Name == "HarvestSeason") { actionToUpdate.HarvestSeason = short.Parse(cell.Value.ToString()); } } else { if (selectedLocation == null) { var location = dataGridViewActions.Rows[e.RowIndex].Cells["Location"].Value as Location; actionToUpdate.LocationId = location.Id; } if (selectedPlant == null) { var plant = dataGridViewActions.Rows[e.RowIndex].Cells["Plant"].Value as Plant; actionToUpdate.PlantId = plant.Id; } var actionType = dataGridViewActions.Rows[e.RowIndex].Cells["ActionType"].Value as ActionType; actionToUpdate.ActionTypeId = actionType.Id; db.Add(actionToUpdate); } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show($"Error when saving: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }