Click here to Skip to main content
15,883,851 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi All Respected Developers!

i have 2 same product in stock table. 1st product quantity is 100 and 2nd is 120 now if a person buy 120 quantity i want to reduce 100 from 1st and other 20 form 2nd row of SQL table but i can't understand how its work.

What I have tried:

C#
<pre>            if (ItemNameTextBox.Text.Trim() != String.Empty)
            {
                DbSQLServer mydb = new DbSQLServer(AppSetting.ConnectionString());
                DataTable ProductTbl = mydb.GetDataList("usp_ProductSelectProdutStockDetails",
                new DbParameter { Parameter = "@ProductName", Value = ItemNameTextBox.Text });


                if (ProductTbl.Rows.Count > 0)
                {
                    DataRow row = ProductTbl.Rows[0];
                    this.stockid = Convert.ToInt32(row["StockID"]);
                    this.ProductId = Convert.ToInt32(row["ProductID"]);
                    CurrentStockTextBox.Text = row["ReminnigQuantity"].ToString();
                    SalesTaxTextBox.Text = row["SalesTax"].ToString();
                    UnitPriceTextBox.Text = row["Price"].ToString();
                    if (QuantityTextBox.Text != string.Empty && Convert.ToInt32(QuantityTextBox.Text) != 0)
                    {
                        this.QuantityfromDb = Convert.ToInt32(row["ReminnigQuantity"]) - Convert.ToInt32(QuantityTextBox.Text);
                    }

                }
                else
                {
                    CurrentStockTextBox.Text = 0.ToString();
                    SalesTaxTextBox.Text = 0.ToString();
                    UnitPriceTextBox.Text = 0.ToString();
                    JIMessageBox.ShowErrorMessage("Product out of Stock");
                }

this works fine but if a person buys 120 the 1st row of SQL is -20.
Note:
this is custom Ado.net framework for CURD opretions.

C#
DbSQLServer mydb = new DbSQLServer(AppSetting.ConnectionString());
                DataTable ProductTbl = mydb.GetDataList("usp_ProductSelectProdutStockDetails",
                new DbParameter { Parameter = "@ProductName", Value = ItemNameTextBox.Text });
Posted
Updated 16-Apr-21 5:19am
v3

Quote:
but i can't understand how its work.

There is no way to teach you Databases design and its management in the scope of the little textbox.
You can get started with a tutorial.
Quote:
i search a lot on internet but I can't find right solution

I fear that without technical background, you have to hire a professional or turn to commercial solution.
Usually, finding the right solution imply selecting solutions close to your need and trying them to see which one will fulfill the needs.
 
Share this answer
 
Comments
Fahim ullah 17-Apr-21 10:26am    
I know about sql queries but i cant understand how use it for this purpose.
i can loop,declare variables,write a subqueries etc
It is possible to do this using a loop - BUT just because you can do something does not mean that you should.

First, a working example. This code snippet will do what you want assuming that you assign id's to each row based on the order they are added to the database. You could just as easily use a datetime as long as you have something that implies order.
SQL
declare @prod table (id int identity(1,1), Product char(1), amount int);
insert into @prod values ('A', 100), ('A', 100);
declare @buy int = 120;
declare @work int;
declare @id int;

WHILE @buy > 0
BEGIN
	SELECT TOP 1 @id = id, @work = amount FROM @prod WHERE Product = 'A' AND amount > 0 ORDER BY id DESC
	IF @work <= @buy 
		UPDATE @prod SET amount = 0 WHERE id = @id
	ELSE
		UPDATE @prod SET amount = amount - @buy WHERE id = @id

	SET @buy = @buy - @work
END
select * from @prod
SQL is a set-based language so you really shouldn't be doing this loop SQL-side but in your UI perhaps.

The principle here is
- Make sure you access the rows in the table in the "correct" order - I have used ORDER BY id in the example, but you could use a datetime column to record when the stock arrived. The DESC ensures LIFO of which more below
- Remove as much as you need from the first row that has a positive amount
- keep removing stock from those rows (in that order) until you have used up all that you wanted to extract - see variable @buy
- "remove" means adjust the stock level (amount in my example) by the maximum possible - hence if the first row encountered does not fulfill the amount then set it to 0

Now to a couple of other points.
- Inventory Management usually handles stock in FIFO order (First In, First Out) so that stock does not get out of date, so I question your logic.
- Having two rows per product on a table only makes sense if you are also capturing a batch number or similar, and you are using this query to "pick" boxes from the inventory in the correct order. If you are just concerned with the amount of each product in your "warehouse" then you would only have ONE row per product and the amount is adjusted accordingly with each sale or delivery.
 
Share this answer
 
Comments
Fahim ullah 18-Apr-21 9:27am    
Dear @CHill60 Your Answer Help me a lot. but can you more improve it? there is some ambiguity. if amount is less then buy the how they sell
I mean if I have 200 items in stock how I sell 500 please improve this in SQL server query side then I implement in C#

Thanks !
CHill60 19-Apr-21 5:37am    
If you only have 200 items in stock then you can't sell 500. In my example, examine the value of the variable @work and you will see that it contains the shortfall

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