private void cmdSave_Click(object sender, EventArgs e) { using (var db = new AdressContext()) { var person = new Person { Name = txtName.Text, Adress = txtAdress.Text, PostNr = txtPostNr.Text, City = txtCity.Text, PhoneNr = txtPhoneNr.Text, Email = txtEmail.Text, Birthday = dtpBirthday.Value }; people.Add(person); db.Persons.Add(person); db.SaveChanges(); lstContacts.Items.Add(person.Name); } txtName.Text = ""; txtAdress.Text = ""; txtPostNr.Text = ""; txtCity.Text = ""; txtPhoneNr.Text = ""; txtEmail.Text = ""; dtpBirthday.Value = DateTime.Now; LoadContacts(); }
private void cmdEdit_Click(object sender, EventArgs e) { try { using (var db = new AdressContext()) { SelectedPerson = (Person)lstContacts.SelectedItem; var editedPerson = db.Persons.Find(SelectedPerson.PersonId); editedPerson.Name = txtName.Text; editedPerson.Adress = txtAdress.Text; editedPerson.PostNr = txtPostNr.Text; editedPerson.City = txtCity.Text; editedPerson.PhoneNr = txtPhoneNr.Text; editedPerson.Email = txtEmail.Text; editedPerson.Birthday = dtpBirthday.Value; db.SaveChanges(); } //Search(txtSearch.Text, people); //txtSearch.Clear(); LoadContacts(); } catch { } }
private void cmdDelete_Click(object sender, EventArgs e) { try { using (var db = new AdressContext()) { SelectedPerson = (Person)lstContacts.SelectedItem; var deletedPerson = db.Persons.Find(SelectedPerson.PersonId); db.Persons.Remove(deletedPerson); db.SaveChanges(); lstContacts.Items.Remove(SelectedPerson.Name); } } catch { } LoadContacts(); }