title: Cannot convert ListBox Item to a Entity Database Class
body: <p>I am building a Point of Sale system on ASP.net. I have a database table called "tblProduct" and I have used Entity Framework to convert it into a class using code first approach. I created a binding list, which holds  items, and have bound the ListBox datasource to the binding list. When a person selects a product, the item goes into the binding list and hence is displayed on the ListBox, everything works correctly up until now. My listbox name is "lbProductsChosen", just for reference.</p>
  
  <pre><code>private static BindingList&lt;tblProduct&gt; blProducts = new BindingList&lt;tblProduct&gt;();
  
  protected void Page_Load(object sender, EventArgs e)
          {
              lbProductsChosen.DataSource = blProducts;
              lbProductsChosen.DataTextField = "Description";
              CreateTab();
              AddProductsToTab();
              ButtonResize();
          }
  </code></pre>
  
  <p>However, while trying to make a "Delete" Button, to try and delete a selected item from the ListBox using:</p>
  
  <pre><code>protected void btnDeleteItem_Click(object sender, EventArgs e)
      {
          tblProduct selectedItem = (tblProduct)lbProductsChosen.SelectedItem;
          blProducts.Remove(selectedItem);
      }
  </code></pre>
  
  <p>It gives me an error and tells me that it "Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'Restoraunt_POS.tblProduct'"</p>
  
  <p>Edit: Here is my button click event, just for clarity. When a product button is pressed, it holds the information in its attributes properties, makes a new instance of the tblProduct class and puts it in the binding list(blProducts).</p>
  
  <pre><code>private void ProductButton_Click(object sender, EventArgs e)
          {
              Button b = (Button)sender;
              string description = b.Attributes["data-name"];
              decimal price = Convert.ToDecimal(b.Attributes["data-price"]);
              int productID = Convert.ToInt32(b.Attributes["data-prodID"]);
              int prodType = Convert.ToInt32(b.Attributes["data-prodType"]);
              tblProduct p = new tblProduct() { Description = description , Price = price , ProductID = productID , ProductType = prodType };
              blProducts.Add(p);
              lbProductsChosen.Items.Clear();
              lbProductsChosen.DataBind();
          }
  </code></pre>