private void Window_Loaded(object sender, RoutedEventArgs e) { try { textBox.Text = ""; using (ContactContext db = new ContactContext()) { dataGrid2.ItemsSource = db.Contacts.ToList(); displayGrid(); //dataGrid2.Focus(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); Close(); } }
private void MenuItem_Click(object sender, RoutedEventArgs e) { try { //ID if (textBox0.Text != "") { co.contactID = Convert.ToInt32(textBox0.Text); } //First Name if (textBox1.Text != "") { co.fname = textBox1.Text; } else { MessageBox.Show("First Name cannot be left empty", "WARNING", MessageBoxButton.OK, MessageBoxImage.Warning); return; } //Last Name if (textBox2.Text != "") { co.lname = textBox2.Text; } else { MessageBox.Show("Last Name cannot be left empty", "WARNING", MessageBoxButton.OK, MessageBoxImage.Warning); return; } Regex reg = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); Match match; //Email if (textBox3.Text != "") { match = reg.Match(textBox3.Text); if (match.Success) { co.email = textBox3.Text; } else { MessageBox.Show("Email not valid", "WARNING", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } else { co.email = textBox3.Text; } //Phone Number co.mobilephone = textBox4.Text; //Birth Date co.birthdate = datepick.Text; //Address co.address = textBox5.Text; //Description co.description = textBox6.Text; //create ContactContext object using (ContactContext db = new ContactContext()) { if (textBox0.Text == "") //Create a new database entry { db.Contacts.Add(co); } else //Update an existing database entry { db.Entry(co).State = EntityState.Modified; } db.SaveChanges(); MessageBox.Show("Database Updated", "INFORMATION", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (Exception) { //MessageBox.Show(ex.ToString()); MessageBox.Show("Error with the Database", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); Close(); } Close(); }