'What is the best way to update a table column based on a result of a sql select query C#?

The original query :

with Tr As (
  SELECT
    DocDtls.Warehouse,
    Transactions.Code,
    DocDtls.zDate,
    Transactions.ID,
    Transactions.QtyIn,
    Transactions.QtyOut,
    Transactions.BalanceAfter
  FROM
    DocDtls
    INNER JOIN Transactions ON DocDtls.[PrimDocNum] = Transactions.[DocNum]
),
formatted_tr as (
  select
    ID,
    Code,
    QtyIn,
    QtyOut,
    BalanceAfter,
    LAG(BalanceAfter, 1, 0) Over (
      partition by Warehouse,
      Code
      order by
        Code,zDate,ID
    ) Prev_BlncAfter
  from
    Tr
  )
select ID,Code,QtyIn,QtyOut,BalanceAfter
,SUM(Prev_BlncAfter + QtyIn)-QtyOut As NewBlncAfter
 from formatted_tr
 group by ID,Code,QtyIn,QtyOut,BalanceAfter;
;

Explaining the idea : Let's say that the query returns all transactions of Item X and there are 10 rows as result , I need to loop through all 10 rows and SET BalanceAfter( for the first transaction QtyIn-QtyOut , Any other transaction (PreviousBalanceAfter+QtyIn)-QtyOut) And so on .

What I've tried : I tried to put the query result in a Datatable then filter it one more time using DataView to get the NewBlncAfter of the DataGridView current row ID only so the Dataview only have one row and save it in a variable - Working well so far - when I try to loop through all rows in my DataGridview and update BalanceAfter I got :

Must Declare Scalar Variable @Newblnc

You can find the whole code in here : My Code

So Is there a direct way to update all transactions BalanceAfter to equal

EDIT #1 : I used @Charliface query and the result was :

enter image description here

I used the old query to compare the results , The BalanceAfter should equal NewBlncAfter in every row .

Edit #2 : Using SUM instead of LAG causing wrong calculation and if I used the query more than once the result in BalanceAfter is multiplied

ID  Code    QtyIn   QtyOut  BalanceAfter
9   100001  20000   0        20000
14  100001  0      6000      40000
21  100001  3500    0        60000
24  100001  0      3000      80000

The main idea and the desired result for example :

ID  Code    QtyIn   QtyOut  BalanceAfter
9   100001  20000   0        20000
14  100001  0      6000      14000
21  100001  3500    0        17500
24  100001  0      3000      14500

The formula is : for the first transaction QtyIn-QtyOut , Any other transaction (PreviousBalanceAfter+QtyIn)-QtyOut And so on .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source