private void DeleteRecordButton_Click(object sender, EventArgs e) { try { var oilAndFilters = dbContext.OilAndFilters.Where(x => x.CarId == Car.Id); var otherServices = dbContext.OtherServices.Where(x => x.CarId == Car.Id); dbContext.OilAndFilters.RemoveRange(oilAndFilters); dbContext.OtherServices.RemoveRange(otherServices); dbContext.CarExtras.Remove(Car.CarExtras); dbContext.Cars.Remove(Car); dbContext.SaveChanges(); MessageBox.Show("Успешно изтрит запис."); var homePage = new HomePageForm(); Close(); homePage.Show(); } catch (Exception ex) { logger.WriteLine($"SearchServiceBookFormOne.DeleteRecordButton_Click: {ex}"); MessageBox.Show("Възникна неочаквана грешка!"); } }
private void BackButton_Click(object sender, EventArgs e) { try { var homePageForm = new HomePageForm(); Close(); homePageForm.Show(); } catch (Exception ex) { logger.WriteLine($"SearchServiceBookFormOne.BackButton_Click: {ex}"); MessageBox.Show("Възникна неочаквана грешка!"); } }
private void EndButton_Click(object sender, EventArgs e) { try { var dateMadeChanges = DateMadeChanges_DatePicker.Value; var kilometers = CurrentKilometers_TextBox.Text.Trim(); var serviceMade = Description_TextBox.Text.Trim(); HomePageForm homePageForm; if (string.IsNullOrWhiteSpace(kilometers) && string.IsNullOrWhiteSpace(serviceMade)) { dbContext.Cars.Add(Car); dbContext.SaveChanges(); homePageForm = new HomePageForm(); Close(); homePageForm.Show(); return; } var sb = new StringBuilder(); var emptyOrWrongFields = new List <string>(); if (string.IsNullOrWhiteSpace(kilometers)) { emptyOrWrongFields.Add("Км"); } if (string.IsNullOrWhiteSpace(serviceMade)) { emptyOrWrongFields.Add("Извършена сервизна дейност"); } if (emptyOrWrongFields.Count() > 0) { sb.AppendLine("Моля попълнете следните полета: "); sb.AppendLine(string.Join(", ", emptyOrWrongFields.Select(x => x))); MessageBox.Show(sb.ToString()); return; } var currentKilometersRegex = new Regex("^([0-9]*)$|^([0-9]* [0-9]*)$"); if (!currentKilometersRegex.IsMatch(kilometers)) { emptyOrWrongFields.Add("Км"); } if (emptyOrWrongFields.Count() > 0) { sb.AppendLine("Моля въведете коректни данни за следните полета: "); sb.AppendLine(string.Join(", ", emptyOrWrongFields.Select(x => x))); MessageBox.Show(sb.ToString()); return; } var toStringedDate = dateMadeChanges.ToString("dd.M.yyyy HH:mm:ss"); var jsonData = new OtherServicesJsonData(toStringedDate, kilometers, serviceMade); var data = JsonConvert.SerializeObject(jsonData); var otherServices = new OtherService(data); Car.OtherServices.Add(otherServices); dbContext.Cars.Add(Car); dbContext.SaveChanges(); homePageForm = new HomePageForm(); Close(); homePageForm.Show(); } catch (Exception ex) { logger.WriteLine($"CreateNewServiceBookFormThree.End_Button_Click: {ex}"); MessageBox.Show("Възникна неочаквана грешка!"); } }