0

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

3
  • Since you're doing it in VB.NET, why not you just sum up the numbers yourself? Commented Sep 17, 2014 at 19:03
  • Where is ds.Tables(0) coming from? If it's a database, it's best to let the DB engine do the aggregation. Commented Sep 17, 2014 at 19:05
  • It is a datatable. I'll use this eventually to pull 10 different pieces of information. If I let the db engine to the aggregation, i'll have to write 10 different queries using the same stored procedure. Commented Sep 17, 2014 at 19:06

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This is great. I used your SUM idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.