/// <summary> /// Metoda koja se poziva prilikom klika na gumb "Spremi promjene" /// (dodaje novi tip/sprema promjene na postojećem tipu korisnika) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSpremiPromjene_Click(object sender, EventArgs e) { // varijabla koja se koristi u slučaju greške (sadržava poruku koja će se prikazati) string poruka = ""; // kod se nalazi unutar try-catch-finally bloka kako bi uspješno detektirali greške kao što su CONSTRAINT Violation try { using (var db = new Entities(Konstante.GetConnectionString())) { if (tipKorisnika == null) { tip_korisnika noviTip = new tip_korisnika() { naziv = tbNazivTipa.Text, opis = tbOpis.Text, omogucen = cbOmogucen.SelectedItem.ToString() }; if (!string.IsNullOrWhiteSpace(tbNazivTipa.Text)) { db.tip_korisnika.Add(noviTip); db.SaveChanges(); } else { poruka = "Type name is mandatory field!"; } } else { db.tip_korisnika.Attach(tipKorisnika); tipKorisnika.naziv = tbNazivTipa.Text; tipKorisnika.opis = tbOpis.Text; tipKorisnika.omogucen = cbOmogucen.SelectedItem.ToString(); if (!string.IsNullOrWhiteSpace(tbNazivTipa.Text)) { db.SaveChanges(); } else { poruka = "Type name is mandatory field!"; } } } } catch (Exception) { poruka = "Error occurred while saving user type (does this user already exist?)"; } finally { if (!string.IsNullOrWhiteSpace(poruka)) { MessageBox.Show(poruka); } else { Close(); } } }
public FrmUrediDodajTip(object _tipKorisnika) { InitializeComponent(); tipKorisnika = (tip_korisnika)_tipKorisnika; }