OrderDetails - Missing?

Feb 24, 2011 at 9:30 AM

Hey guys

Well i've had some problems with the creation of OrderDetails, and have been trying to solve it, but i don't seem to be able to.
When one create an order, the is added info about the customer in the database under the tab "Order".
Besides that, there SHOULD be added futher info about what album ect. in the "OrderDetails" tab, but the problem is, there isnt.

In my case, there is created all the info in the "Order" tab in the database (except from the "total").

Now that is what caught my eye, the "total" tab being "0" in all cases.

So what ive come to test around with is the "ShoppingCart" class under Models. The following code pressents the problem, i just cant figure out the problem, or where it is.

 

public int CreateOrder(Order order)        {            
decimal orderTotal = 0;
var cartItems = GetCartItems();
            // Iterate over the items in the cart, adding the order details for each          
foreach (var item in cartItems)            {                
var orderDetails = new OrderDetail                {                    
AlbumId = item.AlbumId,                    
OrderId = order.OrderId,                    
UnitPrice = item.Album.Price,                    
Quantity = item.Count                
};

                // Set the order total of the shopping cart                
orderTotal += (item.Count * item.Album.Price);            }
            
// Set the order's total to the orderTotal count            
order.Total = orderTotal;
            
// Save the order            
storeDB.SaveChanges();
            
// Empty the shopping cart            
EmptyCart();
            
// Return the OrderId as the confirmation number            
return order.OrderId;        

Like i said, i just can't find the problem, but the is not created anything in the "OrderDetails" tab, in the database, nor is there updated anything about the "total" in the "order" tab.

I really hope someone can help me with this

Apr 5, 2011 at 1:12 PM

hi,I just noticed this bug.

you can solve it by add some code below

storeDB.OrderDetails.Add(orderDetails);

the completed CreateOrder code follow behind:

 public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;
            var cartItems = GetCartItems();
            foreach (var item in cartItems)
            {
                var orderDetails = new OrderDetail
                {
                    AlbumId = item.AlbumId,
                    OrderId = order.OrderId,
                    UnitPrice = item.Album.Price,
                    Quantity = item.Count
                };
                storeDB.OrderDetails.Add(orderDetails); //add order details to database
                orderTotal += (item.Count * item.Album.Price);

            }
            order.Total = orderTotal;
            storeDB.SaveChanges();
            EmptyCart();
            return order.OrderId;
        }

Coordinator
Apr 6, 2011 at 11:22 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.