I have a transaction table and willing to go through each line and check if the running balance is correct.
the logic is: If last line balance -current withdrawal +current deposit =current balance, the line is correct.
I write the following code, and it does highlight the line with wrong calculation.
<Code Start>
using System;
int TotalLines = Context.Field("Balance").Items.Count;
//first, declare the first row with no rule error, checking will be done in the other script
Context.Field("Deposits").Items[0].HasRuleError =false;
Context.Field("Withdrawals").Items[0].HasRuleError =false;
Context.Field("Balance").Items[0].HasRuleError =false;
//declare variable
decimal thisLineBalance = 0;
decimal lastLineBalance = 0;
decimal thisLineWithdrawals = 0;
decimal thisLineDeposit = 0;
for (int i = 1; i < TotalLines ; i++)
{
//get balance, deposit and withdrawals of this and next line
Decimal.TryParse(Context.Field("Balance").Items[i].Text, out thisLineBalance);
Decimal.TryParse(Context.Field("Balance").Items[i-1].Text, out lastLineBalance);
Decimal.TryParse(Context.Field("Withdrawals").Items[i].Text, out thisLineWithdrawals);
Decimal.TryParse(Context.Field("Deposits").Items[i].Text, out thisLineDeposit);
//Start Checking
if(lastLineBalance - thisLineWithdrawals + thisLineDeposit == thisLineBalance)
{
Context.Field("Balance").Items[i].IsVerified = true;
Context.Field("Deposits").Items[i].IsVerified = true;
Context.Field("Withdrawals").Items[i].IsVerified = true;
Context.Field("Balance").Items[i].HasRuleError = false;
Context.Field("Deposits").Items[i].HasRuleError =false;
Context.Field("Withdrawals").Items[i].HasRuleError =false;
}
else//pop error if the running balance is not match
{
Context.CheckSucceeded=false;
Context.ErrorMessage="Line Running Balance do not match.";
Context.Field("Balance").Items[i].IsVerified = false;
Context.Field("Deposits").Items[i].IsVerified = false;
Context.Field("Withdrawals").Items[i].IsVerified = false;
Context.Field("Balance").Items[i].HasRuleError=true;
Context.Field("Deposits").Items[i].HasRuleError =true;
Context.Field("Withdrawals").Items[i].HasRuleError =true;
}
}
<Code End>
My problem are....
First,I have 3 lines having error, none of the error message will disappear until I correct all 3 lines with correct values. I expect while I correct anyone of the line, that line will remove the error message(red box).
Second, After I correct back all values, I intentionally change another 3 lines to wrong values and expect it pop 3 red box respectively.However, the result is only highlight as red box for the first line I adjust the value, but not the other.
Is it some logic error in my code?
It looks really strange and spend me a lot of time already and sincerely look for help.
Thanks