C# decimal.Parse() strange FormatException

1k Views Asked by At

There is MyDataSet myDataSet with MyTable with column string MyNumber (names changed). Then a row is added to the table (existing code):

decimal d = GetSum();
myDataSet.MyTable.AddMyTableRow(d.ToString("F2"));

Now I have to get that decimal number. But it throws FormatException when I try

decimal.Parse(myDataSet.MyTable[0].MyNumber);
// or
decimal.Parse(myDataSet.MyTable[0].MyNumber, CultureInfo.InvariantCulture);
//or
decimal.Parse(myDataSet.MyTable[0].MyNumber, CultureInfo.CurrentCulture)
//or
anything else..
3

There are 3 best solutions below

0
On BEST ANSWER

I solved this by creating additional decimal columns for these numbers.

3
On

I had a similar issue a few days back. This however was with my ModelBinding in a MVC project. I had to overwrite the class responsible for binding decimal values to model variables.

Try to replace the full stop (.) with a comma(,)

0
On

I would guess that a FormatException suggests that you are not putting a string value into the Decimal.Parse method.

http://msdn.microsoft.com/en-us/library/cafs243z.aspx

The MSDN suggests that:

FormatException = s is not in the correct format.

From the looks you are accessing the column itself rather than the data contained in it.