private void btSave_Click(object sender, RoutedEventArgs e) { string makeModel = tbMakeModel.Text; double engineSizeL = slideEngineSizeL.Value; Car.FuelTypeEnum fuelType = (Car.FuelTypeEnum)comboFuelType.SelectedItem; try { if (car == null) { //carList.Add(new Car(makeModel, engineSizeL, fuelType)); Globals.DB.AddCar(new Car(makeModel, engineSizeL, fuelType)); } else { car.MakeModel = makeModel; car.EngineSizeL = engineSizeL; car.FuelType = fuelType; Globals.DB.UpdateCar(car); } } catch (InvalidParameterException ex) { MessageBox.Show(this, ex.Message, "Input Data Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } catch (SqlException ex) { MessageBox.Show(this, ex.Message, "Input Data Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } this.DialogResult = true; }
public List <Car> GetAllCars() { SqlCommand cmdSelectAll = new SqlCommand("SELECT * FROM Cars", sqlConnection); SqlDataReader selectAllReader = cmdSelectAll.ExecuteReader(); List <Car> result = new List <Car>(); while (selectAllReader.Read()) { int id = selectAllReader.GetInt32(selectAllReader.GetOrdinal("Id")); string makeModel = selectAllReader.GetString(selectAllReader.GetOrdinal("MakeModel")); double engineSizeL = selectAllReader.GetDouble(selectAllReader.GetOrdinal("EngineSizeL")); Car.FuelTypeEnum fuelType = (Car.FuelTypeEnum)Enum.Parse(typeof(Car.FuelTypeEnum), selectAllReader.GetString(selectAllReader.GetOrdinal("FuelType"))); result.Add(new Car(id, makeModel, engineSizeL, fuelType)); } selectAllReader.Close(); cmdSelectAll.Dispose(); return(result); }
private void miUpdate_Click(object sender, RoutedEventArgs e) { if (lstViewCar.SelectedItems.Count != 1) { MessageBox.Show("Please choose one item to update", "Information"); return; } try { int index = lstViewCar.SelectedIndex; AddEditDialog addEditDialog = new AddEditDialog((Car)lstViewCar.SelectedItem); addEditDialog.Owner = this; addEditDialog.AddNewCarCallback += (m, en, f) => { model = m; engine = en; fuel = f; }; bool?result = addEditDialog.ShowDialog(); // this line must be stay after the assignment, otherwise value is not assigned if (result == true) { carList.ElementAt(index).MakeModel = model; carList.ElementAt(index).EngineSize = engine; carList.ElementAt(index).FuelType = fuel; } } catch (DataInvalidException ex) { MessageBox.Show(ex.Message, "Error Information"); } }
private void CommandBinding_AddNewCar(object sender, ExecutedRoutedEventArgs e) { try { AddEditDialog addEditDialog = new AddEditDialog(null); addEditDialog.Owner = this; addEditDialog.AddNewCarCallback += (m, en, f) => { model = m; engine = en; fuel = f; }; bool?result = addEditDialog.ShowDialog(); // this line must be stay after the assignment, otherwise value is not assigned if (result == true) { carList.Add(new Car(model, engine, fuel)); } } catch (DataInvalidException ex) { MessageBox.Show(ex.Message, "Error Information"); } lstViewCar.SelectedIndex = lstViewCar.Items.Count - 1;//for status bar selection change }