I am getting following error when trying to convert to Double.
Can not assign ‘output_dt.AsEnumerable.Sum(Function(a) If(String.IsNullOrEmpty(a(“Opening Deposit”).ToString),0,CDbl(a(“Opening Deposit”))))’ to ‘OpeningDeposit’. —> System.InvalidCastException: Conversion from string “$-11,534.00” to type ‘Double’ is not valid
The error arises because the string values in the “Opening Deposit” column contain a currency symbol (“$”) and commas, which aren’t directly parsable as doubles.
CDbl cannot handle these extra characters.
Solution:
Remove Invalid Characters:
Use String.Replace to remove “$” and “,” before conversion:
Dim cleanValue = a("Opening Deposit").ToString().Replace("$", "").Replace(",", "")
Utilize Double.TryParse:
This method attempts conversion and handles potential errors: