private void btnSave_Click(object sender, EventArgs e) { try { //Added Validation for make and model if (string.IsNullOrWhiteSpace(tbMake.Text) || string.IsNullOrWhiteSpace(tbModel.Text)) { MessageBox.Show("Please ensure that you provide a make and a model"); } else { //if(isEditMode == true) if (isEditMode) { //Edit Code here var id = int.Parse(lblId.Text); var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id); car.Model = tbModel.Text; car.Make = tbMake.Text; car.VIN = tbVIN.Text; car.Year = int.Parse(tbYear.Text); car.LicensePlateNumber = tbLicenseNum.Text; } else { //Added validation for make and model of cars being added // Add Code Here var newCar = new TypesOfCar { LicensePlateNumber = tbLicenseNum.Text, Make = tbMake.Text, Model = tbModel.Text, VIN = tbVIN.Text, Year = int.Parse(tbYear.Text) }; _db.TypesOfCars.Add(newCar); } _db.SaveChanges(); _manageVehicleListing.PopulateGrid(); MessageBox.Show("Operation Completed. Refresh Grid To see Changes"); Close(); } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } }
private void save_btn_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(make_tb.Text) || string.IsNullOrWhiteSpace(model_tb.Text)) { MessageBox.Show("Please to ensure make and model is provided:"); } else { //if true if (isEditMode) { //edit code var id = int.Parse(id_lbl.Text); var car = carRentalEntities.TypeOfCars.FirstOrDefault(q => q.id == id); car.Make = make_tb.Text; car.Model = model_tb.Text; car.VIN = vin_tb.Text; car.Year = int.Parse(year_tb.Text); car.LicensePlateNumber = license_tb.Text; } else { //declaration and initialisation of variable var newCar = new TypeOfCar { LicensePlateNumber = license_tb.Text, Make = make_tb.Text, Model = model_tb.Text, VIN = vin_tb.Text, Year = int.Parse(year_tb.Text) }; carRentalEntities.TypeOfCars.Add(newCar); } carRentalEntities.SaveChanges(); _manageVehicleListing.PopulateGrid(); MessageBox.Show("Operation Completed. Refresh Grid to see Changes"); Close(); } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } }
private void btnSaveChanges_Click(object sender, EventArgs e) { try { if (isEditMode) { var id = int.Parse(lblId.Text); var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id); car.Model = tbModel.Text; car.Make = tbMake.Text; car.Year = int.Parse(tbYear.Text); car.VIN = tbVIN.Text; car.LicensePlateNumber = tbLPN.Text; } else { var newCar = new TypesOfCar { LicensePlateNumber = tbLPN.Text, Model = tbModel.Text, Make = tbMake.Text, Year = int.Parse(tbYear.Text), VIN = tbVIN.Text }; _db.TypesOfCars.Add(newCar); } _db.SaveChanges(); _manageVehicleListing.PopulateGrid(); MessageBox.Show("Changes saved"); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }