private void deleteContact_Click(object sender, EventArgs e) { using (var context = new ContactsContext()) { try { var contact = context.Contacts.Find(int.Parse(Id)); context.Contacts.Remove(contact); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show("Please make sure contact has been selected from the list box first!", ""); Console.WriteLine(ex); } catch (ArgumentNullException ex) { MessageBox.Show("Please make sure contact has been selected from the list box first!", ""); Console.WriteLine(ex); } context.Database.Connection.Close(); } }
private void retrieveContactsBtn_Click(object sender, EventArgs e) { using (var context = new ContactsContext()) { try { displayContactsListBox.Items.Clear(); foreach (var c in context.Contacts.Include("PhoneNumbers").ToList()) { displayContactsListBox.Items.Add(c); } } catch (ArgumentNullException ex) { MessageBox.Show("Please add some contacts first!", ""); Console.WriteLine(ex); } context.Database.Connection.Close(); } }
private void updateContact_Click(object sender, EventArgs e) { using (var context = new ContactsContext()) { try { var contact = context.Contacts.Find(int.Parse(Id)); foreach (var c in context.Contacts.Include("PhoneNumbers").ToList()) { if (c.Id == int.Parse(Id)) { contact.FirstName = firstName_textBox.Text; contact.Surname = lastName_textBox.Text; contact.PhoneNumbers[0].Number = phoneNumber_textBox.Text; MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo); context.SaveChanges(); clearFormFields(); } } } catch (ArgumentNullException ex) { MessageBox.Show("All fields must be filled!", ""); Console.WriteLine(ex); } context.Database.Connection.Close(); } }
private void submitData_btn_Click(object sender, EventArgs e) { var contact = new Contact { FirstName = firstName_textBox.Text, Surname = lastName_textBox.Text }; contact.PhoneNumbers.Add( new PhoneNumber { Number = phoneNumber_textBox.Text }); using (var context = new ContactsContext()) { try { context.Contacts.Add(contact); try { context.SaveChanges(); } catch(DbEntityValidationException ex) { MessageBox.Show("Please make sure all fields are filled!", ""); Console.WriteLine(ex); } } catch (ArgumentNullException ex) { MessageBox.Show("All fields must be filled!", ""); Console.WriteLine(ex); } context.Database.Connection.Close(); } }