0

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..
va.
  • 848
  • 3
  • 17
  • 39
  • 1
    What is the value of `myDataSet.MyTable[0].MyNumber` when the exception is thrown? – David Sep 11 '13 at 13:16
  • It looks like normal decimal value, like "1.23" Now I think that maybe the problem is because of that "F2".. ? – va. Sep 11 '13 at 13:17
  • 2
    Is it "like 1.23" or is it actually 1.23? – David Sep 11 '13 at 13:17
  • 1
    What data type is the MyNumber field? Are there any other fields are in the row? Can you give us examples of what exactly is in that field in row 0 when the exception is thrown? – 15ee8f99-57ff-4f92-890c-b56153 Sep 11 '13 at 13:18
  • @user646263 what are the character code points? is it perhaps not a `.` but some other character that looks like a `.` ? – Marc Gravell Sep 11 '13 at 13:18
  • @user646263 - Ideally you should use decimal.TryParse() in your code. – Bibhu Sep 11 '13 at 13:20
  • @Bibhu, why if you know its a decimal? – Ash Burlaczenko Sep 11 '13 at 13:20
  • To get the code-points: `Debug.WriteLine(string.Join(" ", s.Select(c => (int)c)));`, where `s` is the string - maybe `string s = myDataSet.MyTable[0].MyNumber;` – Marc Gravell Sep 11 '13 at 13:21
  • Please paste this line and post the result: (myDataSet.MyTable[0].MyNumber.ToList().ForEach(p => Console.WriteLine("{0}: {1}", p, (int)p)); – Alireza Sep 11 '13 at 13:24
  • You can try: myDataSet.MyTable.AddMyTableRow(d.ToString("F2"), CultureInfo.InvariantCulture); – Nick Bray Sep 11 '13 at 13:25
  • What datatype is myDataSet? Is it a standard "DataSet"? – ImGreg Sep 11 '13 at 13:34
  • try using the NumberFormatInfo class and pass as the Iformatprovider for the parse method. – terrybozzio Sep 11 '13 at 13:34
  • 1
    Try to replace the full stop (.) with a comma (,) , if the conversion succeeds you know that you are still experiencing a culture issue. – Andre Lombaard Sep 11 '13 at 13:37
  • something is going on with either your Data table or your string conversion.. This code works fine DataSet set = new DataSet(); DataTable table = new DataTable(); table.Columns.Add( new DataColumn("test", typeof(string))); set.Tables.Add(table); decimal d = 1.00M; set.Tables[0].Rows.Add(d.ToString("F2")); var val = decimal.Parse(set.Tables[0].Rows[0][0].ToString()); Console.WriteLine(val); – chris warner Sep 11 '13 at 13:43
  • @David - yes it is actually 1.23 but a string. – va. Sep 11 '13 at 14:13
  • @EdPlunkett - it is string field. – va. Sep 11 '13 at 14:13
  • @ImGreg it is typed DataSet. – va. Sep 11 '13 at 14:14
  • Also.. I am checking if it is not NullOrEmpty or DBNull.. When I create a new DataSet like @chriswarner and do the same decimal parsing then it is working correctly (or if I do it with strings only). Looks like there is a problem with the existing dataset, everything that goes through that string field, cannot be parsed back. – va. Sep 11 '13 at 14:15
  • What is the type of myDataSet.MyTable[0].MyNumber? decimal.Parse expects a string, so if you pass it any other type you would probably get a format exception. @user646263 – user65439 just now – Andre Lombaard Sep 11 '13 at 14:40
  • Interesting that if I clone the DataTable and change the type of the Column to decimal, and then import a row in the new DataTable, I can get my decimal value from that new DataTable.. – va. Sep 11 '13 at 15:12

3 Answers3

2

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(,)

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
  • Replacing the full stop with a comma does not help (I checked, it really replaces) Looks like I'll have to add a new decimal column.. – va. Sep 11 '13 at 14:19
  • What is the type of myDataSet.MyTable[0].MyNumber? decimal.Parse expects a string, so if you pass it any other type you would probably get a format exception. @user646263 – Andre Lombaard Sep 11 '13 at 14:39
  • It is a string. And the DataSet is typed. – va. Sep 11 '13 at 15:09
0

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.

ImGreg
  • 2,946
  • 16
  • 43
  • 65
0

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

va.
  • 848
  • 3
  • 17
  • 39