-3

datatable.Compute("Sum([My Money])",""); Error: Invalid usage of aggregate function Sum() and Type: Object.

1 Answers1

0

Check out this code:

Dim table As New DataTable

With table.Columns
    .Add("Integers", GetType(Integer))
    .Add("Objects", GetType(Object))
End With

With table.Rows
    .Add(1, 1)
    .Add(2, 2)
    .Add(3, 3)
End With

Dim integerSum = CInt(table.Compute("SUM(Integers)", Nothing))
Dim objectSum = CInt(table.Compute("SUM(Objects)", Nothing))

If you run that code, guess what happens. The first Compute call works and the second throws an exception with the same error message you report. The issue is exactly what the error message suggests it is: you're trying to sum data of a type that can't logically be summed.

If you want to be able to sum numbers then you need to make sure that you have numbers to sum. I don't know how you created your DataTable and its columns in the first place because you didn't show us but you need to make sure that you do it such that your numerical data is stored in a column with the appropriate numerical data type.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46