public static void OnAddBoatIsPressed(object source, AddBoatEventArgs e) { if (!string.IsNullOrWhiteSpace(e.BoatName) && e.BoatType != null) { try { InputValidation.CheckForInvalidCharacters(e.BoatName); InputValidation.IsYoutubeUrl(e.BoatYoutubeUrl); var boat = new Boat { boatName = e.BoatName, boatTypeId = e.BoatTypeId, boatYoutubeUrl = (e.BoatYoutubeUrl == "")? null : e.BoatYoutubeUrl }; var selectedImageString = BoatImages.ImageToBase64(e.BoatImage, System.Drawing.Imaging.ImageFormat.Png); var selectedImageInput = selectedImageString; var boatImage = new BoatImages { boatImageBlob = selectedImageInput, }; //Check if a boat with this name already exists CheckIfBoatExists(boat); //Add the boat to the database AddBoatToDb(boat); BoatImages.AddImageToDb(boatImage); Switcher.Switch(new boatOverviewScreen(e.FullName, e.AccessLevel, e.MemberId)); } catch (FormatException) { //Warning message for FormatException MessageBox.Show("De ingevulde boot naam is niet geldig\n(let op: speciale tekens zijn niet toegestaan)", "Ongeldige waarde", MessageBoxButton.OK, MessageBoxImage.Warning); } catch (InvalidYoutubeUrlException) { //Warning message for InvalidYoutubeUrlException MessageBox.Show("Vul een geldige YouTube URL in", "Ongeldige URL", MessageBoxButton.OK, MessageBoxImage.Warning); } catch (FileTooLargeException) { MessageBox.Show("De geselecteerde afbeelding is te groot. (Max. 256kb)", "Bestand te groot", MessageBoxButton.OK, MessageBoxImage.Warning); } catch (Exception ex) { //Error message for any other exception that could occur MessageBox.Show(ex.Message, "Een fout is opgetreden", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Vul alle velden in.", "Niet alle velden zijn ingevuld", MessageBoxButton.OK, MessageBoxImage.Warning); } }
//Method that inserts the associated image into the database public static void AddImageToDb(BoatImages boatImage) { using (var context = new BootDB()) { boatImage.boatId = (from b in context.Boats orderby b.boatId descending select b.boatId).FirstOrDefault(); context.BoatImages.Add(boatImage); context.SaveChanges(); } }