Пример #1
0
        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();
        }
Пример #2
0
        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 { }
        }
Пример #3
0
        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();
        }