Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
Let me go directly to my problem. Here is the explanation.
I want to update the value of stock level based on the stock in and stock out. the current quantity of the stock should change if user update the value of the stock in or stock out. Currently, i only can update the value based on the 1 situation whether it is value of stock in or stock out. Is there any possible coding/way that i should try?
this my coding for updating :
XML
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:INVENTORY.MDFConnectionString %>"
            SelectCommand="SELECT Inventory.*, StokIn.QtyIn FROM Inventory INNER JOIN StokIn ON Inventory.MedicineId = StokIn.MedicineId"
            UpdateCommand="UPDATE Inventory SET CurrentQty = Inventory.CurrentQty + StokIn.QtyIn FROM Inventory INNER JOIN StokIn ON Inventory.MedicineId = StokIn.MedicineId">
        </asp:SqlDataSource>


Is there any possibility for me to combine the "-" arithmetic to calculate the current quantity once the user update the Stock out value? Thanks in advance
Posted
Updated 1-Mar-21 9:15am

1 solution

So you want to add the in and subtract the out in a single SQL statement?

Not sure of your schema, but something like this will work
SQL
UPDATE Inventory
SET CurrentQty = Inventory.CurrentQty + IsNull(StokIn.QtyIn, 0) - IsNull(StokOut.QtyOut, 0)
FROM Inventory
 LEFT JOIN StokIn ON Inventory.MedicineId = StokIn.MedicineId
 LEFT JOIN StokOut ON Inventory.MedicineId = StokIn.MedicineId
 
Share this answer
 
Comments
sugababes4 4-Apr-11 1:32am    
thanks MarqW, i'be try the code that u've given. But, the calculation is not very well functioning. For your information, I'M using update function in the gridview. here is my code :

Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim KodUbat As String = CType(Session("KodUbat"), String)

Dim EstrConn As String
EstrConn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\inventory.mdf;Integrated Security=True;User Instance=True "
Dim EMySQL As String = "UPDATE Inventory SET CurrentQTY = Inventory.CurrentQTY + ISNULL(StockIn.QtyIn, 0) - ISNULL(StockOut.QtyOut, 0) FROM Inventori LEFT OUTER JOIN StockIn ON Inventory.MedicineId = StockIn.MedicineId LEFT OUTER JOIN StockOut ON Inventory.MedicineId = StokMasuk.MedicineId"
Dim EMyConn As New SqlClient.SqlConnection(EstrConn)
Dim ECmd As New SqlClient.SqlCommand(EMySQL, EMyConn)
EMyConn.Open()
ECmd.ExecuteNonQuery()
EMyConn.Close()
End Sub

The value of the current quantity should be changed based on the changing of the input value stock in or out.

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