private void btnSave_Click(object sender, EventArgs e)
        {
            string titel      = txtTitel.Text.Trim();
            int    uitgeverId = Convert.ToInt32(cbUitgever.SelectedValue);
            int    publicatie = Convert.ToInt32(nudPublicatie.Value);
            int    score      = Convert.ToInt32(nudScore.Value);
            int    paginas    = Convert.ToInt32(nudPaginas.Value);

            using (BoekenEntities1 ctx = new BoekenEntities1())
            {
                Boeken newBoeken = new Boeken();
                newBoeken.Titel         = titel;
                newBoeken.AantalPaginas = paginas;
                newBoeken.Score         = score;
                newBoeken.Publicatie    = publicatie;
                newBoeken.UitgeverId    = uitgeverId;

                ctx.Boekens.Add(newBoeken);
                ctx.SaveChanges();

                int boekId = newBoeken.Id;



                foreach (var item in lbAuteurs.SelectedItems)
                {
                    BoekenAuteur newBoekenAuteur = new BoekenAuteur();
                    newBoekenAuteur.BoekId   = boekId;
                    newBoekenAuteur.AuteurId = (Int32)item.GetType().GetProperty("Id").GetValue(item);
                    ctx.BoekenAuteurs.Add(newBoekenAuteur);
                    ctx.SaveChanges();
                }

                foreach (var item in lbGenres.SelectedItems)
                {
                    BoekenGenre newBoekenGenre = new BoekenGenre();
                    newBoekenGenre.GenreId = (Int32)item.GetType().GetProperty("Id").GetValue(item);
                    newBoekenGenre.BoekId  = boekId;
                    ctx.BoekenGenres.Add(newBoekenGenre);
                    ctx.SaveChanges();
                }

                MessageBox.Show("Boek toegevoegt");
                this.Close();

                txtTitel.Clear();
                lbAuteurs.SelectedIndex = 0;
                lbGenres.SelectedIndex  = 0;
                nudPaginas.Value        = 200;
                nudPublicatie.Value     = 2000;
                nudScore.Value          = 0;
            }
        }
示例#2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string titel      = txtTitel.Text.Trim();
            int    uitgeverId = Convert.ToInt32(cbUitgever.SelectedValue);
            int    publicatie = Convert.ToInt32(nudPublicatie.Value);
            int    score      = Convert.ToInt32(nudScore.Value);
            int    paginas    = Convert.ToInt32(nudPaginas.Value);
            int    boekId     = Convert.ToInt32(cbBoeken.SelectedValue);

            using (BoekenEntities1 ctx = new BoekenEntities1())
            {
                ctx.Boekens.Where(b => b.Id == boekId).FirstOrDefault().Titel         = titel;
                ctx.Boekens.Where(b => b.Id == boekId).FirstOrDefault().AantalPaginas = paginas;
                ctx.Boekens.Where(b => b.Id == boekId).FirstOrDefault().Score         = score;
                ctx.Boekens.Where(b => b.Id == boekId).FirstOrDefault().Publicatie    = publicatie;
                ctx.Boekens.Where(b => b.Id == boekId).FirstOrDefault().UitgeverId    = uitgeverId;

                ctx.SaveChanges();

                ctx.BoekenGenres.RemoveRange(ctx.BoekenGenres.Where(x => x.BoekId == boekId));

                ctx.BoekenAuteurs.RemoveRange(ctx.BoekenAuteurs.Where(x => x.BoekId == boekId));


                foreach (var item in lbAuteurs.SelectedItems)
                {
                    BoekenAuteur newBoekenAuteur = new BoekenAuteur();
                    newBoekenAuteur.BoekId   = boekId;
                    newBoekenAuteur.AuteurId = (Int32)item.GetType().GetProperty("Id").GetValue(item);
                    ctx.BoekenAuteurs.Add(newBoekenAuteur);
                    ctx.SaveChanges();
                }

                foreach (var item in lbGenres.SelectedItems)
                {
                    BoekenGenre newBoekenGenre = new BoekenGenre();
                    newBoekenGenre.GenreId = (Int32)item.GetType().GetProperty("Id").GetValue(item);
                    newBoekenGenre.BoekId  = boekId;
                    ctx.BoekenGenres.Add(newBoekenGenre);
                    ctx.SaveChanges();
                }

                MessageBox.Show("Boek saved");
                DisplayBoekenDropdown();
                DisplayBoekenDetails();
            }
        }