/// <summary>
        /// Adds new row to DB
        /// </summary>
        private void AddCarBtn_Click(object sender, RoutedEventArgs e)
        {
            //Validation for empty fields
            InputDataValidator(inputPrice, comboModels, comboColors, comboCondition, comboCountry);

            //Set up a new object
            TB_CAR carObj = new TB_CAR()
            {
                ID_CAR_MODEL     = context.TB_CAR_MODEL.Where(model => model.CAR_MODEL == this.comboModels.Text).Select(model => model.ID_CAR_MODEL).FirstOrDefault(),
                ID_CAR_COLOR     = context.TB_CAR_COLOR.Where(color => color.COLOR == this.comboColors.Text).Select(color => color.ID_CAR_COLOR).FirstOrDefault(),
                ID_CAR_CONDITION = context.TB_CAR_CONDITION.Where(condition => condition.CONDITION == this.comboCondition.Text).Select(condition => condition.ID_CAR_CONDITION).FirstOrDefault(),
                ID_CAR_COUNTRY   = context.TB_CAR_COUNTRY.Where(country => country.COUNTRY == this.comboCountry.Text).Select(country => country.ID_CAR_COUNTRY).FirstOrDefault(),
                CAR_PRICE        = int.Parse(this.inputPrice.Text)
            };

            // Add new object to DB
            context.TB_CAR.Add(carObj);
            context.SaveChanges();

            //Reset values after adding
            ResetComboBoxText(comboModels, comboCountry, comboColors, comboCondition);
            this.inputPrice.Text = "";

            //Reload DataGrid
            ReloadGrid();

            ShowInformationMessageBox("Item successfully added!", "Add");
        }
Ejemplo n.º 2
0
 public CarDealerClass(TB_CAR car)
 {
     ID_CAR        = car.ID_CAR;
     CAR_MODEL     = car.TB_CAR_MODEL.CAR_MODEL;
     CAR_COLOR     = car.TB_CAR_COLOR.COLOR;
     CAR_CONDITION = car.TB_CAR_CONDITION.CONDITION;
     CAR_COUNTRY   = car.TB_CAR_COUNTRY.COUNTRY;
     CAR_PRICE     = (int)car.CAR_PRICE;
 }
        /// <summary>
        /// Update selected row in DB and DataGrid
        /// </summary>
        private void UpdateCars(object sender, RoutedEventArgs e)
        {
            InputDataValidator(inputPriceUpdate, comboModelsUpdate, comboCountryUpdate, comboConditionUpdate, comboColorsUpdate);

            CarDealerManagementDBEntities context = new CarDealerManagementDBEntities();

            if (updatingCarID == null)
            {
                ShowInformationMessageBox("Please select a row from the DataGrid to update", "Row not selected");
                return;
            }
            var result = from r in context.TB_CAR where r.ID_CAR == this.updatingCarID select r;

            TB_CAR obj = result.SingleOrDefault();

            //Check for null
            if (obj != null)
            {
                var id = obj.ID_CAR;
                obj.ID_CAR_COLOR     = context.TB_CAR_COLOR.Where(color => color.COLOR == this.comboColorsUpdate.Text).Select(color => color.ID_CAR_COLOR).FirstOrDefault();
                obj.ID_CAR_CONDITION = context.TB_CAR_CONDITION.Where(cond => cond.CONDITION == this.comboConditionUpdate.Text).Select(cond => cond.ID_CAR_CONDITION).FirstOrDefault();
                obj.ID_CAR_COUNTRY   = context.TB_CAR_COUNTRY.Where(country => country.COUNTRY == this.comboCountryUpdate.Text).Select(country => country.ID_CAR_COUNTRY).FirstOrDefault();
                obj.ID_CAR_MODEL     = context.TB_CAR_MODEL.Where(model => model.CAR_MODEL == this.comboModelsUpdate.Text).Select(model => model.ID_CAR_MODEL).FirstOrDefault();
                obj.CAR_PRICE        = decimal.Parse(this.inputPriceUpdate.Text);
                context.SaveChanges();

                //Reset values
                ResetComboBoxText(comboModelsUpdate, comboCountryUpdate, comboColorsUpdate, comboConditionUpdate);
                this.inputPriceUpdate.Text = "";
                updatingCarID = null;
                ManageIsFieldEnabled(false, inputPriceUpdate, comboColorsUpdate, comboModelsUpdate, comboConditionUpdate, comboCountryUpdate);

                ReloadGrid();

                ShowInformationMessageBox("Item successfully updated!", "Update");
            }
        }
        /// <summary>
        /// Removes selected row from the database
        /// </summary>
        private void DeleteCar(object sender, RoutedEventArgs e)
        {
            if (updatingCarID == null)
            {
                ShowInformationMessageBox("Select a row to delete", "No row selected");
                return;
            }

            //Ask user to confirm action
            MessageBoxResult removeResult = MessageBox.Show("Remove selected object from database?",
                                                            "Remove",
                                                            MessageBoxButton.YesNo,
                                                            MessageBoxImage.Warning,
                                                            MessageBoxResult.No);

            if (removeResult == MessageBoxResult.Yes)
            {
                CarDealerManagementDBEntities context = new CarDealerManagementDBEntities();
                var result = from d in context.TB_CAR
                             where d.ID_CAR == this.updatingCarID
                             select d;

                TB_CAR obj = result.SingleOrDefault();

                if (obj != null)
                {
                    context.TB_CAR.Remove(obj);
                    context.SaveChanges();

                    ResetComboBoxText(comboModelsUpdate, comboCountryUpdate, comboColorsUpdate, comboConditionUpdate);
                    this.inputPriceUpdate.Text = "";
                    updatingCarID = null;
                    ReloadGrid();
                }
            }
        }