Conversion from string "" to type 'Integer' is not valid

I will print the average price of the products in each city. My code is like this but I get “Conversion from string “” to type ‘Integer’ is not valid” error.

This is my code:
( From row In dtRead
Let xCityName = row(“City”).ToString.Trim
Group row By a = xCityName Into grp = Group
Let xCount = CStr(grp.Count)
Let xSum = grp.Sum(Function (s) CInt( s(“Price”).ToString.Trim))
Let xAverage = grp.Average(Function (av) CInt(av(“Price”).toString.Trim))
Select dtFinal.Rows.Add({a,xCount,xAverage})).CopyToDataTable

image

Hi,

Instead of cint could you please try with conver.toint32 to convert from string to integer. thanks.

i tried but i got “The input string was not in the correct format.” error

From row In dtRead
Let xCityName = row(“City”).ToString.Trim
Group row By a = xCityName Into grp = Group
Let xCount = CStr(grp.Count)
Let xSum = grp.Sum(Function (s) Convert.Toint32( s(“Price”).ToString.Trim))
Let xAverage = grp.Average(Function (av) Convert.Toint32(av(“Price”).toString.Trim))
Select dtFinal.Rows.Add({a,xCount,xAverage})).CopyToDataTable

Have you tried like above please check.

An empty string cannot be converted into an integer. The reason of this issue could be that a row without value is processed. In excel this could be e.g. a row on end, in which another column with a space is present.

Check for this the datatable content and maybe do a cleansing filtering before processing with the LINQ

1 Like