public void FormDataBind() { ViewState["id"] = Session["id"]; Session.Remove("id"); using (LatihanEntities context = new LatihanEntities()) { int id = Convert.ToInt32(ViewState["id"]); Product product = context.Products.First(x => x.Id == id); if (product != null) { txtId.Text = Convert.ToString(product.Id); txtName.Text = Convert.ToString(product.Name); txtBrand.Text = Convert.ToString(product.Brand); txtCategory.Text = Convert.ToString(product.Category); txtPrice.Text = Convert.ToString(product.Price); } } }
public void bindGrid() //function to bind gridView(Read Operation) { using (LatihanEntities context = new LatihanEntities()) { ProductGrid.DataSource = (from product in context.Products select new { Id = product.Id, Name = product.Name, Brand = product.Brand, Category = product.Category, Price = product.Price }).ToList(); ProductGrid.DataBind(); } }
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"); } }