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 dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex]; var id = (int)row.Cells["ProductID"].Value; if (id > 0) { using (var context = new ProductMasterEntities()) { var obj_New = context.ProductUploads.Where(a => a.ProductID == id).FirstOrDefault(); txtPId.Text = obj_New.ProductID.ToString(); // txtPId.ReadOnly = true; txtPName.Text = obj_New.ProductName; pbbrowse.Image = ByteToImage(obj_New.ProductImage); } } }
//private void Set_db_Object_Value_MASTER(object db_BE, ProductUpload obj_db) //{ // throw new NotImplementedException(); //} private void Load_Data_Grid() { try { var Coll_Data = new List <ProductUpload>(); using (var context = new ProductMasterEntities()) { Coll_Data = context.ProductUploads.ToList(); }; dataGridView1.DataSource = Coll_Data; } catch (Exception ee) { MessageBox.Show(ee.Message); } }
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); } }