0

I have a problem with decimal parse, I know this question been asked a loot, but none of the solution worked for me, and i've been stuck in here for two days now.

My problem is my CultureInfo is set to fr_Fr and when i put the code below an error shows caused with comma that separates decimal instead of period.

double entree = Convert.ToDouble(row["entree"]);
double sortie = Convert.ToDouble(row["sortie"]);
int id_mw = Convert.ToInt32(row["mouvment_w_id"]);

qte_Stock += entree - sortie;
decimal qte_s ;

MessageBox.Show("" + CultureInfo.CurrentCulture);
qte_s = Decimal.Parse(Convert.ToString(qte_Stock), NumberStyles.Number ^ NumberStyles.AllowThousands);

MessageBox.Show("" + qte_s);
qte.CommandText = "Update tcpos.dbo.Warehouse_mouvement set qte_stock= " + qte_s + " where mouvment_w_id = "+id_mw;
qte.ExecuteNonQuery();
Oded
  • 489,969
  • 99
  • 883
  • 1,009
SKGeek
  • 81
  • 2
  • 12
  • 1
    Out of curiosity, why are you xor-ing the two `NumberStyles` together? – lc. Nov 14 '12 at 15:50
  • 4
    Side note - please don't construct SQL queries by string concatenation. Please use parameters (this could also avoid your issue, since that way you never treat the decimal as a string either) – Damien_The_Unbeliever Nov 14 '12 at 15:50
  • 4
    What's the type of `qte_Stock`, and why are you converting it to a string before then converting to decimal? Why go via a string? Additionally, **please** use parameterized SQL instead of embedding values directly into your SQL. – Jon Skeet Nov 14 '12 at 15:50
  • Shouldn't it be NumberStyles.Number | NumberStyles.AllowThousands ? Why from double via string to decimal? – Mithrandir Nov 14 '12 at 15:51
  • Why are you converting a double(?) to decimal via a string? Is there something special you are you trying to accomplish here? – lc. Nov 14 '12 at 15:51
  • I only converted it to a string to use Decimal.Parse – SKGeek Nov 14 '12 at 15:55
  • @SKGeek: Ok, but what was it before, a decimal? – Tim Schmelter Nov 14 '12 at 15:56
  • The error get fired every time the debugger reached the update statement because of the comma that just doesn't want to get change or removed – SKGeek Nov 14 '12 at 15:57
  • @TimSchmelter yes it was a decimal – SKGeek Nov 14 '12 at 15:57
  • @SKGeek: Then you have your answer since you want to make it a decimal. Do nothing and it works (two days for nothing). – Tim Schmelter Nov 14 '12 at 15:58
  • I tried that in the first place , but the comma fired the exception that why i been trying everything and anything and till didn't work – SKGeek Nov 14 '12 at 16:00
  • @SKGeek: Use `Parameters` as Jon already has suggested. That will prevent you from sql-injection and this issue. – Tim Schmelter Nov 14 '12 at 16:01

2 Answers2

1

What's the type of qte_Stock

...

it was a decimal

...

Then you have your answer since you want to make it a decimal.

Pass the decimal as Parameter to prevent sql-injection and this issue(decimal separator).

For example(assuming SQL-Server as rdbms):

using(var con = new SqlConnection(connectionString))
using(var cmd = new SqlCommand("Update tcpos.dbo.Warehouse_mouvement set qte_stock=@qte_stock where mouvment_w_id = @mouvment_w_id", con))
{
    cmd.Parameters.AddWithValue("@qte_stock", qte_stock);
    cmd.Parameters.AddWithValue("@mouvment_w_id", id_mw);
    con.Open();
    cmd.ExecuteNonQuery();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
qte_s = qte_Stock.ToString ("#.#", System.Globalization.CultureInfo.InvariantCulture);

This will solve, if your problem is because in your culture (like mine), the comma seaparates decimals, and not thousands.

Also, use parameterized SQL, like Damien_The_Unbeliever and Jonh Skeet said.

LMB
  • 1,137
  • 7
  • 23