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

                List <Deputaty> deps = db.Deputaties.ToList();
                addForm.comboBox1.DataSource    = deps;
                addForm.comboBox1.ValueMember   = "Partiyniy_bilet";
                addForm.comboBox1.DisplayMember = "Familiya";

                DialogResult result = addForm.ShowDialog(this);

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

                Zakonoproekt zak = new Zakonoproekt();

                zak.Naimenovanie = addForm.textBox1.Text;
                zak.Opisanie     = addForm.richTextBox1.Text;
                zak.Deputaty     = (Deputaty)addForm.comboBox1.SelectedItem;
                zak.Vnesen       = (int)addForm.comboBox1.SelectedValue;

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Zakonoproekts.Add(zak);

                        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();
            }
        }
Exemple #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;
                }

                Zakonoproekt     zak     = db.Zakonoproekts.Find(id);
                ZakonoproektForm addForm = new ZakonoproektForm();

                addForm.textBox1.Text     = zak.Naimenovanie;
                addForm.richTextBox1.Text = zak.Opisanie;

                List <Deputaty> deps = db.Deputaties.ToList();

                addForm.comboBox1.DataSource    = deps;
                addForm.comboBox1.ValueMember   = "Partiyniy_bilet";
                addForm.comboBox1.DisplayMember = "Familiya";

                addForm.comboBox1.SelectedItem = zak.Deputaty;

                DialogResult result = addForm.ShowDialog(this);

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

                zak.Naimenovanie = addForm.textBox1.Text;
                zak.Opisanie     = addForm.richTextBox1.Text;
                zak.Deputaty     = (Deputaty)addForm.comboBox1.SelectedItem;
                zak.Vnesen       = (int)addForm.comboBox1.SelectedValue;

                using (var transaction = db.Database.BeginTransaction()) {
                    try {
                        db.Entry(zak).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();
            }
        }