Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have designed a winforms Application and I have written all the code except one thing has confused me. I am designing something to manage a shop. I have a table 'stock' with columns [ID,Name,Cost_px,Selling_px,Quantity], Another table 'sales' with columns [date,ID,Name,Quantity,px_sold,income]. I have a form(ID,quantity,price_sold) to enter sales and its connected to the 'sales' table.
What can I do so that Quantity in the 'stock' table is reduced automatically by the number of units sold.
eg. product ID=1, price_sold(one unit) = 20000, quantity = 8. this is entered into the 'sales' table, and quantity in the 'stock' table reduced by 8 automatically.
how can I do this.
Thank you.nice tyme.
Posted
Updated 15-Oct-13 4:02am
v2
Comments
Richard MacCutchan 15-Oct-13 10:00am    
Add some code to deduct the number from the stock record.

You could do this backend by using CURSOR[^]. A simple example[^] for you.

FYI
SQL Server Cursor Alternatives[^]

In front end, you have to write logic(inside loop, you have to build the query based on Item & quantity & execute) based on your requirement. But I recommend the backend way.
 
Share this answer
 
You don't say so, but I'll assume that you're storing your data in an SQL database.

When an item (or collection thereof) is sold, as you've said, you need to record the sale but also you need to amend the stock record for that item.

There are two database concepts worth looking at for this:
1. Stored Procedures. A stored procedure allows you to do multiple consecutive database data management steps invoked from one application call. So, where currently your application is calling into the database to record the sale, you'd change it to invoke the 'item sold' stored procedure - which in turn inserts the sale into the sales table and updates the stock record before returning control to your application.

2. Transactions. Transactions ensure that of a set of related database updates if any one fails then none persist - leaving the database in an internally consistent state.

Hope that helps,
Chris
 
Share this answer
 
you must write the code on button click or any other event to reduce the no of units sold using update query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900