private void authorDeleteBtnClick(object sender, RoutedEventArgs e) { var choice = MessageBox.Show("Are you sure?", "Delete author", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No); if (choice == MessageBoxResult.Yes) { author selected = (author)authorDataGrid.SelectedItem; try { using (NET_MD2Entities context = new NET_MD2Entities()) { context.authors.Remove((from a in context.authors where a.au_id == selected.au_id select a).Single()); context.SaveChanges(); authorDataGrid.ItemsSource = context.authors.ToList(); } } catch (Exception exc) { MessageBox.Show("Something went wrong! " + exc.Message); } } }
private void saveBtnClick(object sender, RoutedEventArgs e) { //Validate //Technically, the DB schema allows everything (except for the ID) to be null... bool IsOkay = true; if (nameField.Text == "") { MessageBox.Show("Input a name!"); IsOkay = false; } if (countryField.Text == "") { MessageBox.Show("Input a country!"); IsOkay = false; } if (cityField.Text == "") { MessageBox.Show("Input a city!"); IsOkay = false; } if (!IsOkay) return; //Save try { using (NET_MD2Entities context = new NET_MD2Entities()) { publisher p = IsNew ? new publisher() : context.publishers.Find(this.pub_id); if(IsNew) p.pub_id = Guid.NewGuid().ToString().Substring(0, 4); //Coursemate's advice; p.pub_name = nameField.Text; p.country = countryField.Text; p.city = cityField.Text; p.state = stateField.Text; if (IsNew) { p.pub_info = new pub_info(); p.pub_info.pr_info = infoField.Text; p.pub_info.pub_id = p.pub_id; p.pub_info.publisher = p; context.publishers.Add(p); //Adding references with EF is so easy, I love it! context.pub_info.Add(p.pub_info); } else { p.pub_info.pr_info = infoField.Text; } context.SaveChanges(); this.Close(); } } catch (Exception exc) { MessageBox.Show("Something went wrong! " + exc.Message); } this.Close(); }
private void saveBtnClick(object sender, RoutedEventArgs e) { //Validate bool IsOkay = true; if (fNameField.Text == "") { MessageBox.Show("Input a first name!"); IsOkay = false; } if (lNameField.Text == "") { MessageBox.Show("Input a last name!"); IsOkay = false; } if (phoneField.Text == "") { MessageBox.Show("Input a phone number!"); IsOkay = false; } if (zipField.Text.Length != 5) { MessageBox.Show("Zip must be 5 characters long!"); IsOkay = false; } else if (phoneField.Text.Length != 12) { MessageBox.Show("Phone number must be 12 digits long!"); IsOkay = false; } //Technically, characters are fine as well, the DB schema is weird if (!IsOkay) return; //Save try { using (NET_MD2Entities context = new NET_MD2Entities()) { author a = IsNew ? new author() : context.authors.Find(this.au_id); if (IsNew) { DateTime now = DateTime.Now; string nowString = DateTime.Now.ToString("HHmmssfff"); //Would have used Guid, but the au_id constraint wants numbers only (I know I can delete it) a.au_id = nowString.Substring(0, 3) + "-" + nowString.Substring(3, 2) + "-" + nowString.Substring(5, 4); //Coursemate's advice } a.au_fname = fNameField.Text; a.au_lname = lNameField.Text; a.city = cityField.Text; a.state = stateField.Text; a.address = addressField.Text; a.zip = zipField.Text; a.phone = phoneField.Text; a.contract = (bool)contractField.IsChecked; if (IsNew) { context.authors.Add(a); } context.SaveChanges(); this.Close(); } } catch (Exception exc) { MessageBox.Show("Something went wrong! " + exc.Message); } }
private void saveBtnClick(object sender, RoutedEventArgs e) { //Validate bool IsOkay = true; if (titleField.Text == "") { MessageBox.Show("Input a title!"); IsOkay = false; } if (authorList.Count == 0) { MessageBox.Show("Title must have at least 1 author!"); IsOkay = false; } if (!IsOkay) return; //Save try { using (NET_MD2Entities context = new NET_MD2Entities()) { title t = IsNew ? new title() : context.titles.Find(this.title_id); if (IsNew) t.title_id = Guid.NewGuid().ToString().Substring(0, 4); //Coursemate's advice t.title1 = titleField.Text; t.publisher = context.publishers.Find(((publisher)publisherField.SelectedItem).pub_id); //I can't just add the publisher instance from the list, since it's from a different context t.pub_id = t.publisher.pub_id; try { t.pubdate = DateTime.Parse(publishedField.Text); } catch(Exception exc) { MessageBox.Show("Incorrect date format! " + exc.Message); return; } t.notes = notesField.Text; t.type = IsNew ? "UNDECIDED" : t.type; //Even though the column in the DB has a default value, it somehow doesn't like it when it's not specified (possibly because t.type then is not initialized?) if (IsNew) { title newTitle = context.titles.Add(t); foreach (author a in authorBox.Items) { author au = context.authors.Find(a.au_id); titleauthor ta = new titleauthor(); ta.au_id = a.au_id; ta.title_id = this.title_id; ta.title = newTitle; ta.author = au; newTitle.titleauthors.Add(ta); } } else { t.titleauthors.Clear(); //This is probably more efficient than comparing t.titleauthor to authorList (with the changed authors) and updating the changes foreach (author a in authorList) { author au = context.authors.Find(a.au_id); titleauthor newTa = new titleauthor(); newTa.au_id = a.au_id; newTa.title_id = this.title_id; newTa.author = au; newTa.title = t; t.titleauthors.Add(newTa); } } context.SaveChanges(); } } catch (Exception exc) { MessageBox.Show("Something went wrong! " + exc.Message); } this.Close(); }