title: How to change Replace an Object's Variable using Session in ASP.NET C#
body: <p>I am trying to figure out how I can change up an order by the customer over a web browser. The customer will order an item with product Id (key), the name of product, the price of the product, and the quantity he wants. <strong>I would like to know how I can change his order by replacing the OLD item with the old quantity with the same item with a different quanitity</strong> SO basically clicking to choose the item and placing 2 different quantities he wishes to purchase. The shopping cart holds the Items that are purchased, so I was wondering how I could destroy an OrderItem from the shopping cart then recreate it.</p>
  
  <p>When my code find the key that is already in the shopping cart, then It needs to be destroyed and recreated with a new Quanitity (a text box from the web app).</p>
  
  <pre><code>  protected void btnOrder_Click(object sender, EventArgs e)
  {
      //Check for Shoppingcart object
      // Create first if not there
      if (Session["cart"] == null)
          Session["cart"] = new ShoppingCart();
      int quantity = 0;
  
      // make sure there is text
      if (txtQuantity.Text.Trim().Length != 0)
      {
          quantity = int.Parse(txtQuantity.Text);
  
          if (((ShoppingCart)Session["cart"]).
              keyExists(int.Parse(productID.Text)))
          {
          //Here I should Destroy the current item that exists and replace with new one
  
          }
          else  // This is a new item
          {
              // Make the item
              OrderItem item = new OrderItem(
                  int.Parse(productID.Text), productName.Text,
                  double.Parse(productPrice.Text),
                  int.Parse(txtQuantity.Text));
              // add to cart  
              ((ShoppingCart)Session["cart"]).addToCart(item);
  
          }
          // How does this work? Who is sender?
          this.btnReturn_Click(sender, e);
      }
      else
      {
          Response.Write("Nothing Ordered&lt;br&gt;You must order some of the product or return to the Catalog");
      }
  </code></pre>
  
  <p>Here is the OrderItem object</p>
  
  <pre><code>public class OrderItem
  {
  private int productID;
  private string prodName;
  private double unitPrice;
  private int quantityOrdered;
  
  private string exceptionStr;
  
  
  public OrderItem(int id, string name, double price, int quantity)
  {
      prodName = name;
      exceptionStr = "Numeric data must not be negative";
      if ( id &lt; 0 || price &lt; 0 || quantity &lt; 0)
      {
          throw new System.ArgumentException(exceptionStr);
      }
      else
      {
          productID = id;
          unitPrice = price;
          quantityOrdered = quantity;
      }
  }
  
  
  #region Public Properties
  public int ProductID
  {
      get
      {
          return productID;
      }
  }
  
  public string ProductName
  {
      get 
      {
          return prodName;
      }
  }
  
  
  public double UnitPrice
  {
      get
      {
          return unitPrice;
      }
  }
  
  
  public int QuantityOrdered
  {
      get
      {
          return quantityOrdered;
      }
      set
      {
          if( value &lt; 0 )
          {
              throw new ArgumentException(exceptionStr);
          }
          else
          {
              quantityOrdered = value;
          }
      }
  }
  
  #endregion
  }
  </code></pre>
  
  <p>Here is the Shoppingcart for your viewing:</p>
  
  <pre><code> public class ShoppingCart : IEnumerable
   {
  private SortedList theCart;
  
  public ShoppingCart() {
      theCart = new SortedList();
  } // end of Constructor
  
  
  public bool HasItems {
      get{
          bool hasItems = false;
          if( theCart.Count &gt; 0 )
              hasItems = true;
          return hasItems;
      }
      set {
          // ignore this is read only
      }
  } // end of HasItems
  
  
  public void addToCart(OrderItem item) {
      theCart.Add(item.ProductID, item);
  }// AddToCaArt
  
  /// &lt;summary&gt;
  /// deletes item that is passed
  /// &lt;/summary&gt;
  /// &lt;param name="item"&gt;&lt;/param&gt;
  public void deleteFromCart(OrderItem item)
  {
      theCart.Remove(item.ProductID);
  } // end deleteFromCart
  
  /// &lt;summary&gt;
  /// deletes the item with this id key
  /// &lt;/summary&gt;
  /// &lt;param name="id"&gt;&lt;/param&gt;
  public void deleteFromCart(int id)
  {
      theCart.Remove(id);
  } // end deleteFromCart
  public OrderItem[] getCartContents()
  {
  
  // need to create stuff
      OrderItem[] stuff = null;
      theCart.Values.CopyTo(stuff, 0);
  
      return (stuff);
  } // end getCartContents
  
  
  public bool keyExists(int ID) {
  
      return theCart.ContainsKey(ID);
  }// end keyExists
  
  public ICollection Values
  {
      get
      {
          return theCart.Values;
      }
  }
  
  #region IEnumerable Members
  
  public IEnumerator GetEnumerator()
  {
      return theCart.GetEnumerator();
  }
  
  #endregion
  }
  </code></pre>