Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

c# - Decimal conversion

Hello:
I have a promlem with decimal conversion in C#. I enter some numbers in a text field. And the software adds zeros to the number everytime the number is updated.
Any ideas on thath. The code is as follows:

{code}decimal fastShipFee = System.Convert.ToDecimal(((TextBox)e.Item.Cells[7].FindControl("editFastShipFee")).Text.Replace(",",".").ToString());{/code}

I use the above conversion to read from a text box in a grid cell.

Later on I call the function below:
{code}currentRetailPolicy.masterUpdate(countryName, fixOrderHandling , costPerUnit, costPerLb, costPerSqFt , costMarkUp, activeCountry, fastShipFee);
{/code}
The function writes to a an access database table.
{code}public void masterUpdate(string countryName, decimal fixOrderHandling, decimal costPerUnit, decimal costPerLb, decimal costPerSqFt, decimal costMarkUp, bool activeCountry, decimal fastShipFee)
{
dbOp myDbOp7 = new dbOp();
cmdMasUpt = new System.Data.OleDb.OleDbCommand();
string commandText ="Update sellerRetailPolicy Set fixOrderHandling =" +
fixOrderHandling + ", costPerUnit =" + costPerUnit + ", costPerLb =" + costPerLb +
", costPerSqFt=" + costPerSqFt + ", markUp=" + costMarkUp + ",activeCountry = " + activeCountry + ", fastShipFee = " + fastShipFee + " Where (customer_ID = " + sid +
") AND country_name = '" + countryName + "'";
HttpContext.Current.Response.Write (commandText);
HttpContext.Current.Response.End();
cmdMasUpt.CommandText = commandText;
cmdMasUpt.Connection = myDbOp7.dbCon;
myDbOp7.dbOpen();
System.Data.OleDb.OleDbTransaction trnsact = myDbOp7.dbCon.BeginTransaction();
cmdMasUpt.Transaction = trnsact;
try
{
cmdMasUpt.ExecuteNonQuery();
trnsact.Commit();
}
catch (Exception e)
{
string ErrorMsg = e.Message.ToString();
trnsact.Rollback();
rError callError = new rError();
callError.errorDisplay(ErrorMsg);
}
finally
{
myDbOp7.dbClose();
}
}{/code}

Any ideas how to resolve that.
[2174 byte] By [cema] at [2007-11-11 7:46:16]
# 1 Re: c# - Decimal conversion
With the decimal displaying 0s, you just need to format it for display. Decimal.ToString() has several overloads for this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdecimalclasstostringtopic.asp
PaulMcM at 2007-11-11 21:48:37 >
# 2 Re: c# - Decimal conversion
Thank you for your reply. I have problems with writing it to the database as well. I capture the data (text) in the text box of the web form. Then I write to MS access DB. If the number in the text box is 1.2 it writes database 12.00000.
Would you have any suggestions on this?

By the way - I display the data with data grid.
The data source of the datagrid is a datasource which holds the decimal values.
I bind the data to the grid by

DataBinder.Eval(Container.DataItem, "fastShipFee")
cema at 2007-11-11 21:49:37 >