private void btnDelete_Click(object sender, EventArgs e) { using (var context = new ProductMasterEntities()) { var obj_New_ProductUpload = new ProductUpload(); var pId = Convert.ToInt32(txtPId.Text); var obj_db = context.ProductUploads.Where(a => a.ProductID == pId).FirstOrDefault(); if (obj_db != null) { context.ProductUploads.Remove(obj_db); context.SaveChanges(); Load_Data_Grid(); MessageBox.Show("Product Deleted Successfully"); } } }
private void btnAdd_Click(object sender, EventArgs e) { try { Byte[] bindata = null; if (pbbrowse.ImageLocation != null) { FileStream fs = new FileStream(pbbrowse.ImageLocation.ToString(), FileMode.Open, FileAccess.Read); //Path is image location bindata = new byte[Convert.ToInt32(fs.Length)]; fs.Read(bindata, 0, Convert.ToInt32(fs.Length)); } using (var context = new ProductMasterEntities()) { var obj_New_ProductUpload = new ProductUpload(); var pId = Convert.ToInt32(txtPId.Text); obj_New_ProductUpload.ProductID = pId; obj_New_ProductUpload.ProductName = txtPName.Text; obj_New_ProductUpload.ProductImage = bindata; var obj_db = context.ProductUploads.Where(a => a.ProductID == pId).FirstOrDefault(); if (obj_db == null) { context.ProductUploads.Add(obj_New_ProductUpload); } else { var entry = context.Entry(obj_db); entry.CurrentValues.SetValues(obj_New_ProductUpload); entry.State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); Load_Data_Grid(); } MessageBox.Show("Product Added Successfully!"); } catch (Exception ee) { MessageBox.Show(ee.Message); } }