Pergunta

I'm attempting to convert a string to a DateTime, and on one computer, it works fine, but on another, it doesn't! The computer it works on is running 32 bit windows 7, the computer it doesn't work on is running 64 bit windows 7. Heres the cod:

            for (int i = 0; i < (lines / 5); i++)
            {
                MessageBox.Show(stringArray[(i * 5) + 4]);
                TransactionList.Add(new Transaction
                {
                    TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
                    TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
                    TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
                    Category = stringArray[(i * 5) + 3],
                    HoursSince2013 = Convert.ToDateTime(stringArray[(i * 5) + 4])
                });
            }

The MessageBox is there just to check what string is failing to convert, and it's this, in the first iteration: 10/26/2013 11:58:03 AM

Any clue why this is failing to convert on one of my computers?? I'm completely confused...

Thanks! Nathan

Foi útil?

Solução

Any clue why this is failing to convert on one of my computers??

The most common cause is a difference in culture. Since you're not specifying a specific culture, the current culture on the system is used.

Try using:

HoursSince2013 = Convert.ToDateTime(stringArray[(i * 5) + 4], CultureInfo.InvariantCulture)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top