Question

I have a database access, and one of this field is double-precision. Normally if i set in the textbox 1.71 or 1,71 the field in database should contain 1.71.

But if i execute query the value of acces'field is 171 !!.

public const string QUERY = 
        @"UPDATE TBLART 
          SET TBLART.COST = @cost
          WHERE TBLART.CODE= '1'";

var param = new DynamicParameters();
var cost = totalCost.Replace(',', '.'); //totalCost is a textbox
param.Add("cost", Double.Parse(cost), DbType.Double);

gsmconn.Execute(QUERY, param);

What i wrong ? Thanks.

Was it helpful?

Solution

double.Parse will use the current thread's culture by default. I suspect your current culture uses "." as a grouping separator.

Two options:

  • Continue to use Replace as you are already doing, but then specify CultureInfo.InvariantCulture when parsing
  • Remove the replacement, and just use the current thread's culture when parsing the original value. This relies on the culture being appropriate for the user, but is probably a better solution when that's the case. (Otherwise someone entering "1,234.56" will get a parse error when they expected a value of "just a bit more than 1234".)

OTHER TIPS

If I remember correctly in windows forms you can bind an double property to a textbox and it will automatically take care of parsing and converting. You should not manually do parsing from string to double.

That problem is already solved fro you by the .NET framework.

Another suggestion, your data access code should not do any parsing. That should be done in some higher layer. But better leave it to the framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top