private void btnAddToCart_Click(object sender, EventArgs e) { if (!ConnectionManager.IsAuthenticated) { MessageBox.Show("Please login before adding to cart.", "Authentication required", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //add to cart if (this.listViewBeers.SelectedItems == null || this.listViewBeers.SelectedItems.Count == 0) { return; } var customerID = ConnectionManager.ActiveCustomerId; var context = new ProductContext(); var activeCart = context.GetActiveCart(customerID); var selectedItems = this.listViewBeers.SelectedItems; var selectedbeer = selectedItems[0].Tag as StandardBeer; int quantity = 1; if (activeCart != null) { ShoppingCartDetail cartDeatil = context.GetItemFromShoppingCart(activeCart.shoppingCartID, selectedbeer.beerID); if (cartDeatil != null) { quantity += cartDeatil.quantityOrdered; context.UpdateCart(cartDeatil.shoppingCartDetailsID, quantity); return; } } context.AddToCart(customerID, activeCart != null ? activeCart.shoppingCartID : -1, selectedbeer, quantity); }