datatable.Compute("Sum([My Money])",""); Error: Invalid usage of aggregate function Sum() and Type: Object.
Asked
Active
Viewed 633 times
-3
-
What is the data type of that column? – jmcilhinney May 07 '21 at 03:11
-
Hi, it seem to be Double, but I dont know exactly. 4,124,175.00 / 307,447.00/ 300,000.00/ 6,700,000.00/ 3,005,000.00/ – hung pham ngoc May 07 '21 at 03:18
-
It doesn't matter what it seems to be. It matters what it is. If you don't know, look. – jmcilhinney May 07 '21 at 03:21
-
I'm sorry, but I'm very new. This data I got from an excel file. Do you get any Idea to fix this. – hung pham ngoc May 07 '21 at 03:25
-
It "seems" to be a double, but does it have the commas in too ? (So it's a string then) – Caius Jard May 07 '21 at 08:05
-
To find the type of a DataTable column `Dim typeName = dt.Columns("My Money").DataType.Name` – Mary May 07 '21 at 19:42
1 Answers
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