private void silToolStripMenuItem_Click(object sender, EventArgs e) { if (lstUrunler.SelectedItem == null) { return; } var urunId = (lstUrunler.SelectedItem as ProductViewModel).ProductID; var cevap = MessageBox.Show("Secili urunu silmek istiyor musunuz?", "Urun Silme", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (cevap != DialogResult.Yes) { return; } try { NorthwindSabahEntities db = new NorthwindSabahEntities(); var urun = db.Products.Find(urunId); db.Products.Remove(urun); MessageBox.Show($"{db.SaveChanges()} kayit silindi"); KategorileriGetir(); } catch (DbUpdateException ex) { MessageBox.Show("Silmek istediginiz kayit baska bir tabloda kullanildigi icin silemezsiniz"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnKategoriKaydet_Click(object sender, EventArgs e) { try { ep1.Clear(); NorthwindSabahEntities db = new NorthwindSabahEntities(); db.Categories.Add(new Category() { CategoryName = string.IsNullOrEmpty(txtKategoriAdi.Text) ? null : txtKategoriAdi.Text, Description = txtAciklama.Text }); int sonuc = db.SaveChanges(); MessageBox.Show($"{sonuc} kayit eklendi"); KategorileriGetir(); } catch (DbEntityValidationException ex) { foreach (var validationError in ex.EntityValidationErrors) { foreach (var error in validationError.ValidationErrors) { if (error.PropertyName == "CategoryName") { ep1.SetError(txtKategoriAdi, error.ErrorMessage); } } } MessageBox.Show(EntityHelper.ValidationMessage(ex), "Bir hata olustu", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnUrunGuncelle_Click(object sender, EventArgs e) { try { ep1.Clear(); NorthwindSabahEntities db = new NorthwindSabahEntities(); var seciliUrun = lstUrunler.SelectedItem as ProductViewModel; var urun = db.Products.Find(seciliUrun.ProductID); urun.ProductName = txtUrunAdi.Text; urun.UnitPrice = nuFiyat.Value; urun.CategoryID = (cmbUrunKategori.SelectedItem as CategoryViewModel).CategoryID; int sonuc = db.SaveChanges(); KategorileriGetir(); MessageBox.Show($"{sonuc} urun guncellendi"); } catch (DbEntityValidationException ex) { foreach (var validationError in ex.EntityValidationErrors) { foreach (var error in validationError.ValidationErrors) { if (error.PropertyName == "ProductName") { ep1.SetError(txtUrunAdi, error.ErrorMessage); } } } MessageBox.Show(EntityHelper.ValidationMessage(ex), "Bir hata olustu", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show(ex.Message); } }