-2

I am using a basic console app with windows task scheduler.

The program takes some decimal value from the net and saves in the SQL Server table.

When I run the program manually there is no problem with the decimal format.

If task scheduler runs the program it reads them (i am guessing here) as int.

Example:

  • while I run the program: 3.7658 => 3.7658
  • Windows Task Scheduler: 3.7654 => 37658

I tried converting it to string replacing "," with ".".

decimal valueKur = 0;

valueKur = Convert.ToDecimal(xmlDoc.SelectSingleNode(string.Format("Tarih_Date/Currency[@Kod='{0}']/BanknoteSelling", item)).InnerXml);

mevcutKur.KurValue = valueKur;
db.SaveChanges();

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Battlelamb
  • 97
  • 7

1 Answers1

5

It sounds like you've used string concatenation to update the data in the database. Basically: don't ever do that. If you use a SQL parameter (SET whatever=@whatever, etc), then the database won't need to do any string parsing, and there will be no confusion - especially over i18n/l10n issue like whether comma/period is a group separator or decimal point. Likewise, store it as a decimal type in the database (not a string/char/etc type), read it out as a decimal type, etc.

When you treat things as text, everything is dependent on your locale - and the locale of the scheduler is not necessrily the locale you had in mind.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • If i wasn't clear i am sorry about that. I already tried without converting to string. I am using entity framework. So there is no sql parameters – Battlelamb Mar 01 '18 at 15:17
  • @Battlelamb EF isn't going to get confused about this, as long as the SQL column is actually a decimal type; you're going to need to show some code at some point - somewhere perhaps in parsing / formatting code. – Marc Gravell Mar 01 '18 at 15:22
  • I just edited my question.:)) – Battlelamb Mar 01 '18 at 15:27
  • @Battlelamb there we go - `Convert.ToDecimal` is a locale-dependent conversion and most likely is the cause of everything; consider specifying an explicit `CultureInfo` in that call – Marc Gravell Mar 01 '18 at 15:43