Example #1
0
        //UPPDATERA ENBART ADRESS
        private void UpdateButton_Click(object sender, EventArgs e)
        {
            using (AdressBokContext context = new AdressBokContext())
            {
                //Hämta adressIdet
                var adress = context.Adresses.Find(gvShowAdresses.SelectedRows[0].Cells["Id"].Value);

                //Lägg till dom upppdaterade värdena
                adress.City       = CityTextBox.Text;
                adress.PostCode   = PostnummerTextBox.Text;
                adress.StreetName = StreetTextBox.Text;

                context.SaveChanges();

                MessageBox.Show("Uppdaterat adress!");

                ShowContactAdresses(int.Parse(gvShowContacts.SelectedRows[0].Cells["Id"].Value.ToString()));
            }
        }
Example #2
0
        //ta bort ennbart ADRESS
        private void RemoveButton_Click(object sender, EventArgs e)
        {
            using (AdressBokContext context = new AdressBokContext())
            {
                var removeAddress = this.gvShowAdresses.SelectedRows[0].Cells[0].FormattedValue.ToString();
                int remov         = int.Parse(removeAddress);
                var removeThis    = context.Adresses.First(x => x.Id == remov);

                var removeCont = int.Parse(gvShowContacts.SelectedRows[0].Cells[0].Value.ToString());

                var contact = context.Contacts.Find(removeCont);

                contact.Adresses.RemoveAll(a => a.Id == remov);



                //context.Adresses.Remove(removeThis);
                context.SaveChanges();
                ShowContactAdresses(removeCont);
            }
        }
Example #3
0
        //Lägg till Kontakt
        private void AddButton_Click(object sender, EventArgs e)
        {
            if (ContactTypeComboBox.SelectedItem == null)
            {
                MessageBox.Show("Ange kontakttyp!", "Error!");
                return;
            }

            using (AdressBokContext context = new AdressBokContext())
            {
                var newAdress = new Adress();
                newAdress.StreetName = StreetTextBox.Text;
                newAdress.PostCode   = PostnummerTextBox.Text;
                newAdress.City       = CityTextBox.Text;

                var cType = context.ContactTypes.SingleOrDefault(c => c.Name == ContactTypeComboBox.Text);

                var newContact = new Contact
                {
                    Name     = NameTextBox.Text,
                    Email    = EmailTextBox.Text,
                    Phone    = PhoneTextBox.Text,
                    Adresses = new List <Adress> {
                        newAdress
                    },
                    ContactTypes = new List <ContactType> {
                        cType
                    }
                };

                context.Contacts.Add(newContact);
                context.SaveChanges();

                LaddaKontakter();
            }

            MessageBox.Show("Kontakt skapad!", "Success!");
        }
Example #4
0
        //Lägg till adress till den specifika kontakten
        private void btnLäggTillAdress_Click(object sender, EventArgs e)
        {
            //Lägg till en adress till den valda kontakten
            Contact contact;

            using (AdressBokContext context = new AdressBokContext())
            {
                contact = context.Contacts.Find(gvShowContacts.SelectedRows[0].Cells["Id"].Value);

                Adress nyadress = new Adress();
                nyadress.PostCode   = PostnummerTextBox.Text;
                nyadress.StreetName = StreetTextBox.Text;
                nyadress.City       = CityTextBox.Text;

                contact.Adresses.Add(nyadress);


                context.SaveChanges();

                MessageBox.Show("Lagt till ny adress!");

                ShowContactAdresses(contact.Id);
            }
        }