示例#1
0
        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;
        }
示例#2
0
        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);
        }
示例#3
0
        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");
            }
        }
示例#4
0
        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
        }