Reputation: 1573
I have a vb.net program that runs a stored procedure on a database. After I build the dataset, i'm scanning the table inside the dataset searching for specific information. I used Select Case statements to find the correct info. The problem i'm running into is I can't figure out how to display my results as a "sum". I know that it's because i'm using "For Each" but i'm unsure how to assign "Result" to ds.tables(0).Rows
Code:
For Each Result As DataRow In ds.Tables(0).Rows
Select Case Result("Report")
Case "TOTALS"
Select Case Result("Description")
Case "Coupons", "Coupons Tax-Free", "GC"
MsgBox(Result("netAmt"))
End Select
End Select
Next
As coded i'm receiving 3 message boxes. The first one:
"15"
The Second One:
"10"
The Third:
"5"
I'd prefer to see the sum of all 3 found:
"30"
How can I retrieve the sum of these, keeping in mind it's possible that one of the 3 comes back as 0 in some cases
Upvotes: 0
Views: 46
Reputation: 6238
You need an additional variable which will store a sum e.g.:
Dim numberOfStudents As Integer = 0
Then instead of displaying the current net amount add it to the current sum:
sum = sum + FoundRow("netAmt")
To simplify I assumed that FoundRow
returns a number. However, if it returns for example a string you will have to parse it in the following way: Integer.Parse(FoundRow("netAmt"))
.
Finally, after the for each loop, display the result:
MsgBox(sum)
Upvotes: 2