protected void btnAdd_Click(object sender, EventArgs e) { List<LineaVenta> carrito = new List<LineaVenta>(); LineaVenta nuevaLineaVenta = new LineaVenta(); LineaVenta antiguaLineaVenta = null; Session["state"] = "modified"; try { nuevaLineaVenta.ProductId = int.Parse(this.tbxProduct.Text); nuevaLineaVenta.Quantity = int.Parse(this.tbxQuantity.Text); if (!(Session["myCart"] == null)) { carrito = (List<LineaVenta>)Session["myCart"]; } foreach (LineaVenta lineaVenta in carrito) { if (nuevaLineaVenta.ProductId == lineaVenta.ProductId) { nuevaLineaVenta.Quantity += lineaVenta.Quantity; antiguaLineaVenta = lineaVenta; } } if (nuevaLineaVenta.Quantity < ProductLogic.GetProducto(nuevaLineaVenta.ProductId).UnitsInStock) { if (antiguaLineaVenta != null) { carrito.Remove(antiguaLineaVenta); } carrito.Add(nuevaLineaVenta); Session["myCart"] = carrito; } else { Session["state"] = "inputError"; } Response.Redirect("~/Catalog.aspx"); } catch (FormatException) { Session["state"] = "inputError"; Response.Redirect("/Catalog.aspx"); } }
protected void btnBorrar_Click(object sender, EventArgs e) { LineaVenta lineaVentaEliminada = new LineaVenta(); try { int idLinea = int.Parse(tbxLineaVenta.Text.Replace("PageContent_", "")); List<LineaVenta> carrito = (List<LineaVenta>)Session["myCart"]; foreach (LineaVenta lineaVenta in carrito) { if (lineaVenta.ProductId == idLinea) { lineaVentaEliminada = lineaVenta; } } carrito.Remove(lineaVentaEliminada); if (carrito.Count == 0) { Session["myCart"] = null; } else { Session["myCart"] = carrito; } Response.Redirect("~/Cart.aspx"); } catch (NullReferenceException) { Session["state"] = "deleteError"; } catch (FormatException) { Session["state"] = "deleteError"; } finally { Response.Redirect("~/Cart.aspx"); } }