0
I am using a while to read data from a spreadsheet and store in a list. When executing the code I get the following error:
Unable to cast object of type 'System.String' to type 'System.Double'.
Pointing to the Indicator line2.
Obs.: I am using Closedxml; I made sure that in the worksheet lines there were no items that were strings; when using code outside of while it works perfectly.
while (true)
{
if (string.IsNullOrEmpty(PlanilhaTwo.Cell("A" + linha2.ToString()).ToString())) break;
//Lendo as células
double IndicadorA1 = (double)Planilha.Cell("C" + linha.ToString()).Value;
double IndicadorB1 = (double)Planilha.Cell("D" + linha.ToString()).Value;
double IndicadorA2 = (double)PlanilhaTwo.Cell("C" + linha2.ToString()).Value;
double IndicadorB2 = (double)PlanilhaTwo.Cell("D" + linha2.ToString()).Value;
double Erro = 6371 * Math.Acos(Math.Cos((Math.PI / 180) * (90 - IndicadorA1)) * Math.Cos((Math.PI / 180) * (90 - IndicadorA2)) + Math.Sin((Math.PI / 180) * (90 - IndicadorA1)) * Math.Sin((Math.PI / 180) * (90 - IndicadorA2)) * Math.Cos((Math.PI / 180) * (IndicadorB1 - IndicadorB2)));
PlanA.Add(new PlanilhaTemp1((double)Planilha.Cell("A" + linha.ToString()).Value, (double)Planilha.Cell("B" + linha.ToString()).Value, (double)PlanilhaTwo.Cell("A" + linha2.ToString()).Value, "vinculado", Erro));
linha2++;
}
the message is very clear, you came to see what value is being passed at the moment the error occurs?
– Marcos Junior
the value is the number -> -21,154896
– Vitor Silva
I believe it’s by the comma, you applying the cast that way the c# expects a correct double value. What you can try is to exchange the (double) for a Convert.Todouble
– Marcos Junior
did not work, I believe it is related to variable Linha2, when I run the same code without incrementing this variable the system works perfectly
– Vitor Silva