Error Input string was not in a correct format when trying to sum up the values of a data table column

Hi,

I’m using since a while a LINQ formula that I found here on the forum to sum up all the values in a column of a data table.

The formula is:
math.Round(ML_FilteredDT.AsEnumerable.Sum(Function(a)Convert.ToDouble(a(“ColumnName”).ToString)),2)

The formula was always working, now, suddendly, I’m receiving the error message that “the input string is not in a correct format”, but I changed nothing in the data table…

Please I need help, because the automation is already in production and I need to fix this asap…

Thank you a lot in advance,
Franz

Hi @franz.verga,

Please check the datatable column values, it seems column value not an integer.

Regards,
Arivu

Hi @franz.verga

Use this and try once

math.Round(ML_FilteredDT.AsEnumerable().Where(Function(d) Not String.IsNullOrEmpty(d("columname").ToString) or Not String.IsNullOrWhiteSpace(d("columname").ToString)).Sum(Function(a)Convert.ToDouble(a("ColumnName").ToString)),2)

Regards
Sudharsan

Thank you this is the solution! The data table is coming from an Excel file and the column is the result of formulas that put empty strings when no values are available…
I’m using for testing and older version of the file. Now I forced them to replace the empty string with 0… But your formula will be really helpful in the future.

Thanks a lot.
Franz

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.