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 ürünü 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 istediğiniz akyit baska 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, //boş kaydetmesin diye 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.Error); } 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); // referans tipindeki urunun propertylerini değiştirip savechange ile değişiklikleri sql e aktarıyor urun.ProductName = txtUrunAdi.Text; urun.UnitPrice = nudFiyat.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.Error); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }