Problem with Adding Shopping Cart Items in BL

Posts   
 
    
Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 19-May-2005 19:46:06   

Here is the code for the method I use to add / update of shopping cart items. What I want it to do is:

if new product then add else if product existing as cart item then update the quantity

It does not currently update properly, any thoughts?

Also, the table for the shopping cart items is:

ShoppingCartID int, PK ProductID int, PK Quantity




        public bool AddShoppingCartItem( int shoppingCartID, int productID, int quantity )
        {
            // Make sure parameters are valid.
            if ( shoppingCartID == 0 || productID == 0 || quantity == 0 )
            {
                return false;
            }

            try
            {
                // Declare the Data Adapter.
                using ( DataAccessAdapter da = new DataAccessAdapter() )
                {
                    // Set the _shoppingCartItemEntity to Dirty = 
                    _shoppingCartItemEntity.IsNew = true;
                    _shoppingCartItemEntity.IsDirty = true;


                    // Set the Primary Key Values.
                    _shoppingCartItemEntity.ShoppingCartId = shoppingCartID;
                    _shoppingCartItemEntity.ProductId = productID;
                    
                    // Does the item already exist?
                    if ( da.FetchEntity( _shoppingCartItemEntity )  )
                    {
                        _shoppingCartItemEntity.Quantity += quantity;
                    }
                    else
                    {
                        _shoppingCartItemEntity.Quantity = quantity;
                    }                   

                
                    // Save the _shoppingCartItemEntity.
                    da.SaveEntity( _shoppingCartItemEntity, false );

                }           
            }
            catch ( Exception ex )
            {
                throw new BizException( "SolutionDesigners.Biz.Shopping.CreateShoppingCartItem( int shoppingCartID, int productID, int quantity ) Exception - " + ex.Message );
            }

            return true;
        }

Thanks, Wade rage

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 20-May-2005 11:58:12   

What does "It doesn't currently update properly" mean? As in: quantity isn't updated properly in the code or SaveEntity fails? Could you elaborate on that a bit?

Your code uses a member variable. Is this shopping cart shared among multiple threads?

Frans Bouma | Lead developer LLBLGen Pro