//this method will clear/reset form values
 private void CleanUp()
 {
     //shopping cart = a new empty list
     ShoppingCart = new List<Product>();
     //Textboxes and labels are set to defaults
     TxtProdCode.Text = string.Empty;
     txtQty.Text = string.Empty;
     lbtotal.Content = "Total: $0.00";
     //DataGrid items are set to null
     CartGrid.ItemsSource = null;
     CartGrid.Items.Refresh();
     //Tmp variable is erased using null
     tmpProduct = null;
 }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //we first check if a product has been selected
            if (tmpProduct == null)
            {
                //if not we call the search button method
                Button_Click_1(null, null);
                //we check again if the product was found
                if (tmpProduct == null)
                {
                    //if tmpProduct is empty (Product not found) we exit the procedure
                    MessageBox.Show("No product was selected", "No product", MessageBoxButton.OK,
                        MessageBoxImage.Exclamation);
                    //exit procedure
                    return;
                }
            }

            //product quantity
            int qty;

            // we try to parse the number of the textbox if the number is invalid
            int.TryParse(txtQty.Text, out qty);
            //if qty is 0 we assign 0 otherwise we assign the actual parsed value
            qty = qty == 0 ? 1 : qty;
            //really basic validation that checks inventory
            if (qty <= tmpProduct.Qty)
            {
                //we check if product is not already in the cart if it is we remove the old one
                ShoppingCart.RemoveAll(s => s.Id == tmpProduct.Id);
                //we add the product to the Cart
                ShoppingCart.Add(new Product()
                {
                    Id = tmpProduct.Id,
                    Name = tmpProduct.Name,
                    Price = tmpProduct.Price,
                    Qty = qty
                });

                //perform  query on Shopping Cart to select certain fields and perform subtotal operation
                BindDataGrid();
                //<----------------------
                //cleanup variables
                tmpProduct = null;
                //once the products had been added we clear the textbox of code and quantity.
                TxtProdCode.Text = string.Empty;
                txtQty.Text = string.Empty;
                //clean up current product label
                cprod.Content = "Current product N/A";
            }
            else
            {
                MessageBox.Show("Not enough Inventory", "Inventory Error", MessageBoxButton.OK,
                    MessageBoxImage.Exclamation);
            }
        }
 private void Button_Click_1(object sender, RoutedEventArgs e)
 {
     //If a product code is not empty we search the database
     if (Regex.IsMatch(TxtProdCode.Text.Trim(), @"^\d+$"))
     {
         DBInvoiceSample db = new DBInvoiceSample();
         //parse the product code as int from the TextBox
         int id = int.Parse(TxtProdCode.Text);
         //We query the database for the product
         Product p = db.Products.SingleOrDefault(x => x.Id == id);
         if (p != null) //if product was found
         {
             //store in a temp variable (if user clicks on add we will need this for the Array)
             tmpProduct = p;
             //We display the product information on a label
             cprod.Content = string.Format("ID: {0}, Name: {1}, Price: {2}, InStock (Qty): {3}", p.Id, p.Name, p.Price, p.Qty);
         }
         else
         {
             //if product was not found we display a user notification window
             MessageBox.Show("Product not found. (Only numbers allowed)", "Product code error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
         }
     }
 }