0

I have a table from database where values in cells are string type: 1.4141, 2.012, 11.33, And Im trying to parse them to double,but it throws FormatException (saying something like wrong entry format).There is a part of the code:

double a;
foreach (DataRow row in ds.Tables[0].Rows)
            {
                a = Double.Parse(row[1].ToString());//here it throws the 
                    exception
            }
RTX
  • 97
  • 7
  • 3
    Most likely you're using comma as your decimal separator, can you verify this? Also, try `Double.Parse(row[1].ToString(), CultureInfo.InvariantCulture);` – Lasse V. Karlsen Jun 29 '17 at 19:17
  • 1
    Also, just out of curiosity, what does `row[1].GetType()` give you? – Lasse V. Karlsen Jun 29 '17 at 19:17
  • Show us the content of `row[1]` – Emond Jun 29 '17 at 19:17
  • 1
    if it was a double, it would be roundtrip-safe: double.ToString() ==> double.Parse() so OP's claim that it is string makes sense. It must be the culture-specific decimal separator, or maybe a missing value or invalid value in some row(s) – Cee McSharpface Jun 29 '17 at 19:19
  • The parsing of the string might not be needed at all depending on how the data is read from the database. – Emond Jun 29 '17 at 19:21
  • no,no,each value has it own cell in table cell,and row[1] is String type – RTX Jun 29 '17 at 19:22
  • 1
    It's also possible that your data contains an invalid value (if these are just strings in the DB). You won't really know what's wrong without looking at the value of `row[1].ToString()` that is failing. – Jon B Jun 29 '17 at 19:23
  • all cells have values like this:1.088 – RTX Jun 29 '17 at 19:23
  • 1
    then try what @Lasse suggests in the first comment, and if it still does not work edit the details into your question. – Cee McSharpface Jun 29 '17 at 19:24
  • It works with CultureInfo.InvariantCulture,thanks to all,problem solved – RTX Jun 29 '17 at 19:38

0 Answers0