public ClientManager(TB_CLIENT client) { ID = client.ID_CLIENT; Name = client.NAME; Surname = client.SURNAME; PESEL = client.PESEL; NIP = client.NIP.ToString() ?? ""; StreetNumber = client.TB_ADDRESS.STREET_NUMBER; City = client.TB_ADDRESS.CITY; ZIPCODE = client.TB_ADDRESS.ZIP_CODE; }
/// <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> /// 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; } }