Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (operacija == "Insert")
                {
                    Servis noviServis = new Servis
                    {
                        KlijentID    = (int)comboBox1.SelectedValue,
                        DatumServisa = dateTimePicker1.Value,
                        OpisUsluge   = textBox1.Text
                    };

                    dc.Servisi.Add(noviServis);

                    dc.SaveChanges();

                    MessageBox.Show("Servis uspesno upisan.");
                }

                else
                {
                    if (operacija == "Update")
                    {
                        Servis updateServis = dc.Servisi.SingleOrDefault(s => s.ServisID == ServisID);

                        updateServis.DatumServisa = dateTimePicker1.Value;
                        updateServis.OpisUsluge   = textBox1.Text;

                        dc.SaveChanges();

                        MessageBox.Show("Servis uspesno izmenjen.");
                    }

                    else
                    {
                        MessageBox.Show("Greska.");
                    }
                }
            }

            catch (DbEntityValidationException EFex)
            {
                foreach (DbEntityValidationResult s in EFex.EntityValidationErrors)
                {
                    foreach (DbValidationError x in s.ValidationErrors)
                    {
                        MessageBox.Show(x.ErrorMessage);
                    }
                }
                dc.Dispose();
                dc = new DBModel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #2
0
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridServisi.SelectedRows.Count == 0)
                {
                    MessageBox.Show("Morate izabrati servis.");
                }

                else
                {
                    int ServisID = (int)gridServisi.SelectedRows[0].Cells[0].Value;

                    Servis DelServis = dc.Servisi.SingleOrDefault(s => s.ServisID == ServisID);

                    dc.Servisi.Remove(DelServis);
                    dc.SaveChanges();

                    MessageBox.Show("Servis uspesno izbrisan.");

                    button5_Click(null, null);
                }
            }

            catch (DbEntityValidationException EFex)
            {
                foreach (DbEntityValidationResult s in EFex.EntityValidationErrors)
                {
                    foreach (DbValidationError x in s.ValidationErrors)
                    {
                        MessageBox.Show(x.ErrorMessage);
                    }
                }
                dc.Dispose();
                dc = new DBModel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }