-1

Our Production Database(SQL Server 2008R2) in Iceland stores sqlmoney columns values with comma(,) as decimal separator (Example 54,12 & 85,00). Our Application (C#,ASP.Net) reading values using SqlDatareader as code give below.

decimal testVal=dr.GetDecimal(idxAmountUSD)//IdxAmountUSD is sqlmoney value

Now it is not considering comma(,) as decimal separator and returns as 5412 & 8500.

Actually it should return as 54.12 & 85.12.

H H
  • 263,252
  • 30
  • 330
  • 514
mastan
  • 95
  • 1
  • 10
  • possible duplicate of [C# decimal separator?](http://stackoverflow.com/questions/3870154/c-sharp-decimal-separator) – Filip Ekberg Aug 13 '12 at 07:38
  • 4
    You're not using `ToDecimal` (which would suggest `Convert.ToDecimal`), you're using `GetDecimal`. That shouldn't involve any string-to-decimal conversion, that shouldn't involve any decimal separator. What does your query look like? Does it return a decimal type, or a string? –  Aug 13 '12 at 07:39
  • sqlmoney is a CLR type. What is the definition of the actual column in the Db? – H H Aug 13 '12 at 07:41
  • 1
    @FilipEkberg No, not a duplicate of that, this isn't about displaying the decimal, it's about the decimal getting the wrong value. –  Aug 13 '12 at 07:41
  • It should return decimal value. And DB column type is money. – mastan Aug 13 '12 at 08:51
  • Did you actually test this piece of code (in the debugger)? This simply cannot happen when GetDecimal is applied to SqlMoney. It probably goes wrong when you format to string later. – H H Aug 13 '12 at 10:13

2 Answers2

0

You need to define your culture before you make an instance of the SQLDataReader:

Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("yourCulture");
XN16
  • 5,679
  • 15
  • 48
  • 72
-2

You need to read the data culture specific, define the culture first and then read the data

Thread.CurrentThread.CurrentCulture = new CultureInfo("is-IS"); //is-IS is an iceland culture (where , is used inplace of .) .
// following line remains same
decimal testVal=dr.GetDecimal(idxAmountUSD)//IdxAmountUSD is sqlmoney value

I did not test the above code, but logically it is correct.

Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101