protected void savebtn_Click(object sender, EventArgs e) { using (LatihanEntities context = new LatihanEntities()) { if (ViewState["id"] != null) { int id = Convert.ToInt32(ViewState["id"]); Product product = context.Products.First(x => x.Id == id); product.Name = Convert.ToString(txtName.Text); product.Brand = Convert.ToString(txtBrand.Text); product.Category = Convert.ToString(txtCategory.Text); product.Price = Convert.ToDecimal(txtPrice.Text); context.SaveChanges(); } else { Product product = new Product(); product.Name = Convert.ToString(txtName.Text); product.Brand = Convert.ToString(txtBrand.Text); product.Category = Convert.ToString(txtCategory.Text); product.Price = Convert.ToDecimal(txtPrice.Text); context.Products.Add(product); context.SaveChanges(); } } Response.Redirect("ProductInformation.aspx"); }
protected void Product_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "editcmd") //code for edit button { Session["id"] = e.CommandArgument; Response.Redirect("SaveOrEditProduct.aspx"); } if (e.CommandName == "deletecmd") //code for delete button { using (LatihanEntities context = new LatihanEntities()) { int id = Convert.ToInt32(e.CommandArgument); context.Products.Remove(context.Products.First(X => X.Id == id)); context.SaveChanges(); } Response.Redirect("SaveOrEditProduct.aspx"); } }