Hi
I have a table in my DB called Products.
Products has various columns but in particular one named ProductId and one named UnitsInStock.
ProductId starts at 1 and increments by 1 and I have a total of 2000 products.
UnitInStock holds the amount of stock for each product ie 5000, 6000 and so on.
I have a SubmitOrder method which persists the data from the ShoppingCart back to the DB in particular the Quantity.
What I would like to do is implement a solution in order to minus the Quantity from the UnitsInStock to enable me to display this on each of the product details.
What I was thinking was to get the UnitsInStock for each ProductId from the Products table and then get the Quantity placed from the Order Details table then subtract one from the other to get a total.
My approach is to get the ProductId and Quanity from the SubmitOrder method ie
OrderDetail od = new OrderDetail();
var getProductId = od.ProductId;
var getQuantity = od.Quantity;
Then somehow call on the Products table getting the UnitsInStock for the particular ProductId passed in and then subtract the Quantity from the UnitsInStock.
Im guessing some sort of Query will be required but im stuck on how to write this.
Thanks
Steven
Loading
VulpesPosted May 6, 2012, 11:27 AM
With no great confidence, I'd suggest the highlighted changes:
Sorry, I was referring earlier to decrementing the Quantity field of the OrderDetails entities when I should, of course, have been referring to decrementing the UnitsInStock field of the Products entities by the corresponding Quantity field.
Guest UserPosted May 6, 2012, 11:42 AM
Many thanks indeed for all your help and patience : )
Regards
Steven
Guest UserPosted May 6, 2012, 11:15 AM
Thanks
Steven
Guest UserPosted May 6, 2012, 11:08 AM
How does this look to you?
Thanks
Steven
Guest UserPosted May 6, 2012, 10:55 AM
So to wrap this up once and for all. This is my SubmitOrder method.
Could you be so kind as to add the necessary code, as you quote
" You just have to iterate though all your OrderDetails entities and change the Quantity field (or property) to its new value using normal C# code. When you've finished that you then update the actual database table by calling the SaveChanges method. "
Regards
Steven
VulpesPosted May 6, 2012, 10:47 AM
You just have to iterate though all your OrderDetails entities and change the Quantity field (or property) to its new value using normal C# code. When you've finished that you then update the actual database table by calling the SaveChanges method.
Guest UserPosted May 6, 2012, 10:33 AM
So given:-
SELECT Products.UnitsInStock, OrderDetails.Quantity, OrderDetails.ProductID
FROM OrderDetails INNER JOIN
Products ON OrderDetails.ProductID = Products.ProductID
ORDER BY Products.UnitsInStock ASC
UPDATE Products SET UnitsInStock = UnitsInStock - OrderDetails.Quantity WHERE ProductID = OrderDetails.ProductID
How and where could I implement this into the SubmitOrder method (obviously before the db.SaveChanges() call).
I dont know how I would encapsulate and place this so it is called and fired off to the DB?
Im guessing I should use something like the following before the db.SaveChanges() call:-
var updateQuantityFromUnitsInStock = (from, where, select); ?
Regards
Steven
VulpesPosted May 6, 2012, 9:35 AM
I'm not very familiar with EF as I don't use it but I'd have thought that you'd need to decrement the Quantity field in your OrderDetails entities and then call SaveChanges on the ObjectContext object to update the database itself.
Guest UserPosted May 6, 2012, 5:21 AM
Could you advise me on how to implement this in C# please (im using Entity Framework as my Data Model).
Shall I place this code in a Stored Proceedure, the SubmitOrder Method or a new Method entirely and if so id need input parameters etc.
If I was to go the Stored Proceedure route then I should of included it when I generated my EF Model so I think its too late for that... Therefore if anything can be done in C# that would be my best bet.
In Future ill know to include everything DB related in my EF! : (
Regards
Steven
VulpesPosted May 5, 2012, 6:45 PM
Guest UserPosted May 5, 2012, 5:31 PM
So far ive managed to construct the following
This is now returning the results I would expect to see as follows:-
UnitsInStock Quantity ProductID
3000 30 8
What I would like to add to the Query is the functionality which deducts the Quantity from the UnitsInStock.
Thanks
Steven