Beispiel #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString()))
            {
                DeputatyForm addForm = new DeputatyForm();

                List <Sozyvy> sozes = db.Sozyvies.ToList();
                addForm.listBox1.DataSource    = sozes;
                addForm.listBox1.ValueMember   = "Nomer";
                addForm.listBox1.DisplayMember = "Nomer";

                List <Frakciya> frakces = db.Frakciyas.ToList();
                addForm.comboBox1.DataSource    = frakces;
                addForm.comboBox1.ValueMember   = "ID";
                addForm.comboBox1.DisplayMember = "Naimenovanie";

                List <Komitet> kometes = db.Komitets.ToList();
                addForm.comboBox2.DataSource    = kometes;
                addForm.comboBox2.ValueMember   = "ID";
                addForm.comboBox2.DisplayMember = "Naimenovanie";

                DialogResult result = addForm.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                Deputaty dep = new Deputaty();
                dep.Partiyniy_bilet = (int)addForm.numericUpDown1.Value;
                dep.Familiya        = addForm.textBox2.Text;
                dep.Imya            = addForm.textBox1.Text;
                dep.Otchestovo      = addForm.textBox3.Text;
                dep.Region_vyborov  = addForm.textBox4.Text;

                dep.Data_rozhdeniya = DateTime.Parse(addForm.maskedTextBox1.Text);

                sozes.Clear(); // очищаем список и заново заполняем его выделенными элементами
                foreach (var soz in addForm.listBox1.SelectedItems)
                {
                    sozes.Add((Sozyvy)soz);
                }
                dep.Sozyvies = sozes;

                dep.Frakciya   = (Frakciya)addForm.comboBox1.SelectedItem;
                dep.ID_frakcii = (int)addForm.comboBox1.SelectedValue;

                dep.Komitet     = (Komitet)addForm.comboBox2.SelectedItem;
                dep.ID_komiteta = (int)addForm.comboBox2.SelectedValue;

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Deputaties.Add(dep);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (DbUpdateException ex)
                        {
                            MessageBox.Show("Ошибка добавления : " + ex.ToString());
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка добавления : " + ex.ToString());

                        try { transaction.Rollback(); }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Ошибка отката : " + ex2.ToString());
                        }
                    }
                }
                RefreshData();
            }
        }
Beispiel #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString()))
            {
                if (dataGridView1.SelectedRows.Count < 1)
                {
                    return;
                }

                int  index     = dataGridView1.SelectedRows[0].Index;
                int  id        = 0;
                bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                if (converted == false)
                {
                    return;
                }

                Deputaty dep = db.Deputaties.Find(id);

                DeputatyForm addForm = new DeputatyForm();

                addForm.numericUpDown1.Value = dep.Partiyniy_bilet;
                addForm.textBox2.Text        = dep.Familiya;
                addForm.textBox1.Text        = dep.Imya;
                addForm.textBox3.Text        = dep.Otchestovo;
                addForm.textBox4.Text        = dep.Region_vyborov;
                addForm.maskedTextBox1.Text  = dep.Data_rozhdeniya.ToString();

                // получаем список команд
                List <Sozyvy> sozes = db.Sozyvies.ToList();
                addForm.listBox1.DataSource    = sozes;
                addForm.listBox1.ValueMember   = "Nomer";
                addForm.listBox1.DisplayMember = "Nomer";

                List <Frakciya> frakces = db.Frakciyas.ToList();
                addForm.comboBox1.DataSource    = frakces;
                addForm.comboBox1.ValueMember   = "ID";
                addForm.comboBox1.DisplayMember = "Naimenovanie";

                List <Komitet> kometes = db.Komitets.ToList();
                addForm.comboBox2.DataSource    = kometes;
                addForm.comboBox2.ValueMember   = "ID";
                addForm.comboBox2.DisplayMember = "Naimenovanie";

                foreach (Sozyvy s in dep.Sozyvies)
                {
                    addForm.listBox1.SelectedItem = s;
                }

                addForm.comboBox1.SelectedItem = dep.Frakciya;
                addForm.comboBox2.SelectedItem = dep.Komitet;

                DialogResult result = addForm.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                dep.Partiyniy_bilet = (int)addForm.numericUpDown1.Value;
                dep.Familiya        = addForm.textBox2.Text;
                dep.Imya            = addForm.textBox1.Text;
                dep.Otchestovo      = addForm.textBox3.Text;
                dep.Region_vyborov  = addForm.textBox4.Text;

                dep.Data_rozhdeniya = DateTime.Parse(addForm.maskedTextBox1.Text);

                sozes.Clear(); // очищаем список и заново заполняем его выделенными элементами
                foreach (var soz in addForm.listBox1.SelectedItems)
                {
                    sozes.Add((Sozyvy)soz);
                }
                dep.Sozyvies = sozes;

                foreach (var s in sozes)
                {
                    if (addForm.listBox1.SelectedItems.Contains(s))
                    {
                        if (!dep.Sozyvies.Contains(s))
                        {
                            dep.Sozyvies.Add(s);
                        }
                    }
                    else
                    {
                        if (dep.Sozyvies.Contains(s))
                        {
                            dep.Sozyvies.Remove(s);
                        }
                    }
                }

                dep.Frakciya   = (Frakciya)addForm.comboBox1.SelectedItem;
                dep.ID_frakcii = (int)addForm.comboBox1.SelectedValue;

                dep.Komitet     = (Komitet)addForm.comboBox2.SelectedItem;
                dep.ID_komiteta = (int)addForm.comboBox2.SelectedValue;

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Entry(dep).State = EntityState.Modified;

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Ошибка обновления : " + ex.ToString());
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка обновления : " + ex.ToString());
                        try { transaction.Rollback(); }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Ошибка отката : " + ex2.ToString());
                        }
                    }
                }
                RefreshData();
            }
        }