/// <summary> /// Delete record from the database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteTransaction(object sender, RoutedEventArgs e) { if (updatingTransactionID == 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_TRANSACTIONS where d.ID_TRANSACTION == this.updatingTransactionID select d; TB_TRANSACTIONS obj = result.SingleOrDefault(); if (obj != null) { context.TB_TRANSACTIONS.Remove(obj); context.SaveChanges(); ResetFieldValue(displayCarBox, displayClientBox, datepickerDisplayTransactionDate); updatingTransactionID = null; ReloadGrid(); } } }
/// <summary> /// Delete row from database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteAddress(object sender, RoutedEventArgs e) { if (updatingAddressID == 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_ADDRESS where d.ID_ADDRESS == this.updatingAddressID select d; TB_ADDRESS obj = result.SingleOrDefault(); if (obj != null) { context.TB_ADDRESS.Remove(obj); context.SaveChanges(); ResetFieldValue(textboxStreetUpdate, textboxCityUpdate, textboxZIPUpdate); updatingAddressID = null; ReloadGrid(); } } }
/// <summary> /// Add record to database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddClient(object sender, RoutedEventArgs e) { bool validation = InputDataValidator(textboxName, textboxSurname, textboxPESEL, textboxNIP, comboAddress); if (validation) { CarDealerManagementDBEntities db = new CarDealerManagementDBEntities(); int parsed; bool parseNIP = int.TryParse(this.textboxNIP.Text, out parsed); string[] addressSplitted = (this.comboAddress.Text).Split(','); //Must assign value to variable , because LINQ Entities does not support 'ArrayIndex' string splitted0 = addressSplitted[0]; foreach (string address in addressSplitted) { address.Trim(); } var add = db.TB_ADDRESS .Where(address => (address.STREET_NUMBER == splitted0)) .Select(adres => adres.ID_ADDRESS) .FirstOrDefault(); Console.WriteLine(add); TB_CLIENT client = new TB_CLIENT() { NAME = this.textboxName.Text.Trim(), SURNAME = this.textboxSurname.Text.Trim(), PESEL = this.textboxPESEL.Text.Trim(), NIP = parseNIP ? parsed : 0, ID_CLIENT_ADDRESS = db.TB_ADDRESS.Where(address => address.STREET_NUMBER == splitted0) .Select(adres => adres.ID_ADDRESS) .FirstOrDefault() }; // Add new object to DB db.TB_CLIENT.Add(client); db.SaveChanges(); ResetFieldValue(this.textboxName, this.textboxSurname, this.textboxPESEL, this.textboxNIP); ReloadGrid(); } }
/// <summary> /// Add record to the database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddTransaction(object sender, RoutedEventArgs e) { CarDealerManagementDBEntities db = new CarDealerManagementDBEntities(); //ComboBox SelectedIndex property counts from 0, so we add 1 to match ID TB_TRANSACTIONS transaction = new TB_TRANSACTIONS() { ID_CAR = db.TB_CAR.Where(car => car.ID_CAR == this.comboboxCar.SelectedIndex + 1).Select(car => car.ID_CAR).FirstOrDefault(), ID_CLIENT = db.TB_CLIENT.Where(client => client.ID_CLIENT == this.comboboxClient.SelectedIndex + 1).Select(client => client.ID_CLIENT).FirstOrDefault(), TRANSACTION_DATE = (DateTime)(this.datepickerTransaction.SelectedDate) }; // Add new object to DB db.TB_TRANSACTIONS.Add(transaction); db.SaveChanges(); ResetFieldValue(this.comboboxCar, this.comboboxClient, this.datepickerTransaction); ReloadGrid(); }
/// <summary> /// Add new record to database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddAddress(object sender, RoutedEventArgs e) { bool validation = InputDataValidator(textboxStreet, textboxCity, textboxZIP); if (validation) { CarDealerManagementDBEntities db = new CarDealerManagementDBEntities(); TB_ADDRESS address = new TB_ADDRESS() { STREET_NUMBER = this.textboxStreet.Text, CITY = this.textboxCity.Text, ZIP_CODE = this.textboxZIP.Text }; // Add new object to DB db.TB_ADDRESS.Add(address); db.SaveChanges(); ResetFieldValue(this.textboxStreet, this.textboxCity, this.textboxZIP); ReloadGrid(); } }
/// <summary> /// Update a record in database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateAddress(object sender, RoutedEventArgs e) { bool validation = InputDataValidator(textboxStreetUpdate, textboxCityUpdate, textboxZIPUpdate); if (validation) { CarDealerManagementDBEntities db = new CarDealerManagementDBEntities(); if (updatingAddressID == null) { ShowInformationMessageBox("Please select a row from the DataGrid to update", "Row not selected"); return; } var result = from r in db.TB_ADDRESS where r.ID_ADDRESS == this.updatingAddressID select r; TB_ADDRESS obj = result.SingleOrDefault(); //Check for null if (obj != null) { //dodać walidacje czy user wprowadza poprawne dane var id = obj.ID_ADDRESS; obj.STREET_NUMBER = this.textboxStreetUpdate.Text; obj.CITY = this.textboxCityUpdate.Text; obj.ZIP_CODE = this.textboxZIPUpdate.Text; db.SaveChanges(); //Reset values ResetFieldValue(this.textboxStreetUpdate, this.textboxCityUpdate, this.textboxZIPUpdate); updatingAddressID = null; ManageIsFieldEnabled(false, textboxStreetUpdate, textboxStreetUpdate, textboxZIPUpdate); ReloadGrid(); ShowInformationMessageBox("Item successfully updated!", "Update"); } } }
/// <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(); } } }
/// <summary> /// Delete row from database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteClient(object sender, RoutedEventArgs e) { if (updatingClientID == 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_CLIENT where d.ID_CLIENT == this.updatingClientID select d; TB_CLIENT obj = result.SingleOrDefault(); if (obj != null) { context.TB_CLIENT.Remove(obj); context.SaveChanges(); ResetFieldValue(textboxNameUpdate, textboxSurnameUpdate, textboxPESELUpdate, textboxNIPUpdate); this.comboAddressUpdate.Text = ""; updatingClientID = null; ReloadGrid(); } } }
/// <summary> /// Update selected row in DB and DataGrid /// </summary> private void UpdateClient(object sender, RoutedEventArgs e) { bool validation = InputDataValidator(textboxNameUpdate, textboxSurnameUpdate, textboxPESELUpdate, textboxNIPUpdate, comboAddressUpdate); if (validation) { CarDealerManagementDBEntities db = new CarDealerManagementDBEntities(); if (updatingClientID == null) { ShowInformationMessageBox("Please select a row from the DataGrid to update", "Row not selected"); return; } var result = from r in db.TB_CLIENT where r.ID_CLIENT == this.updatingClientID select r; TB_CLIENT obj = result.SingleOrDefault(); //Check for null if (obj != null) { var id = obj.ID_CLIENT; obj.NAME = this.textboxNameUpdate.Text.Trim(); obj.SURNAME = this.textboxSurnameUpdate.Text.Trim(); obj.PESEL = this.textboxPESELUpdate.Text.Trim(); int parsedInt; if (int.TryParse(this.textboxNIPUpdate.Text.Trim(), out parsedInt)) { obj.NIP = parsedInt; } else { ShowInformationMessageBox("Input must be type of integer!", "Not an integer"); } string[] addressSplitted = (this.comboAddressUpdate.Text.Trim()).Split(','); //Must assign value to variable, because LINQ Entities does not support 'ArrayIndex' string splitted0 = addressSplitted[0]; obj.ID_CLIENT_ADDRESS = db.TB_ADDRESS.Where(address => address.STREET_NUMBER == splitted0) .Select(adres => adres.ID_ADDRESS) .FirstOrDefault(); db.SaveChanges(); //Reset values ResetFieldValue(this.textboxNameUpdate, this.textboxNameUpdate, this.textboxNameUpdate, this.textboxNameUpdate); this.comboAddressUpdate.Text = ""; updatingClientID = null; ManageIsFieldEnabled(false, textboxNameUpdate, textboxNameUpdate, textboxNameUpdate, textboxNameUpdate, comboAddressUpdate); ReloadGrid(); ShowInformationMessageBox("Item successfully updated!", "Update"); } } else { ShowInformationMessageBox("Some fields contain errors, please check them", "Error"); return; } }