From: Dean H. <dea...@dl...> - 2008-04-07 23:22:04
|
Rolf Marsh wrote: > CultureInfo ci = CultureInfo.CurrentCulture; > NumberFormatInfo nfi = ci.NumberFormat; > decimal decPrice, decCost; > > decPrice = decimal.Parse(pricePieces[0]); // pricePieces is a string > array where I remove trailing currency sign There are two problems here. This will actually, but you've also given us this code: > CultureInfo ci = CultureInfo.CurrentCulture; > NumberFormatInfo nfi = ci.NumberFormat; > decimal decPrice, decCost; > string x = > pricePieces[0].Replace(nfi.CurrencyDecimalSeparator, "."); > decPrice = decimal.Parse(x); Which will not work -- as you say, with a non-US culture (or at least, one that expects "," as the decimal separator, this'll throw an exception. Do not use this code, use the code above (where you just call "decimal.Parse" on the string itself). The second problem is this: > string fbInsertString = "insert into tBooks values ('" + > BookNbr + "', '" + > Title.ToString().Replace("'", "''") + "', '" + > Author.ToString().Replace("'", "''") + "', '" + > ISBN + "', '" + > Illus + "', '" + // > Locn + "', '" + > decPrice + "', '" + > decCost + "', '" + ... The code: string s = "blah" + decimal + "blah"; is equivalent to: string s = "blah" + decimal.ToString() + "blah"; By default, "ToString" uses the current culture, so this code would be using a comma as the decimal separator on a Finnish computer. Firebird only supports a period "." as the decimal separator, and that's why it doesn't work. The correct method, as Jiri points out, is to use parameterised queries (parameterised queries have many other benefits as well... you should never compose strings of SQL like that.) Here's a simple example: FbCommand cmd = new FbCommand( "INSERT INTO tBooks (price) VALUES (@price)", connection); cmd.Parameters.AddWithValue("@price", decPrice); cmd.ExecuteNonQuery(); Dean. |