[英]Parsing string to decimal throws a FormatException for DateTime
我有一個包含不同工具數據的 Web 項目,我可以在 RadGrid(連接到數據庫)中插入和刪除工具,但是我在使用編輯命令時遇到問題
工具的值之一是“成本”,它是十進制的,當我嘗試編輯時,有一半的時間會出現此錯誤:
“輸入字符串格式不正確錯誤”
當用戶點擊編輯表單模式提交編輯時,這是我的編輯方法
protected void btnMdlEditar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal;
var costo = editCosto.Text;
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
try
{
var id = CDatos.DHerramientas.UpdateHerramientas(int.Parse(txtId.Text), editCodigo.Text, editNombreCorto.Text, editDescripcion.Text, costoDecimal, editConsumible.Text);
rgHerramientas.DataBind();
rgHerramientas.Rebind();
upHerramientas.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex) { this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramientaEdit");
}
在行
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
是我收到錯誤的地方,彈出 FormatException,在將字符串解析為 DataTime 時提到一個問題,出於某種原因。 我不知道為什么當我使用確切的代碼從我的插入方法中的字符串解析十進制時會收到此錯誤
protected void btnAutorizar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal;
var costo = txtCosto.Text;
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
try
{
var id = CDatos.DHerramientas.InsertHerramientas(txtCodigo.Text, txtNombreCorto.Text, txtDescripcion.Text, costoDecimal, txtConsumible.Text);
this.upDatos.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex){this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramienta");
}
當我通過僅刪除數字的一部分或未更改數字來編輯數據時會引發錯誤,但是如果我完全刪除數字並輸入另一個十進制數字,則允許我對其進行編輯
7.85 to 7.89 = Error
7.85 to 6.12 = No error
我已經有了
使用 System.Globalization;
我現在的文化是“es-ES”
編輯:我試圖從
costoDecimal = decimal.Parse(costo.ToString(), style, provider); 如果我只更改數字的一部分,則允許我成功編輯,但如果我使用“。”鍵入新數字。 它再次向我拋出錯誤
我認為問題是使用“。” 有沒有辦法同時使用“,”,而不是“,”,這是西班牙用於小數的方法。 和 ”,”?
明白了,問題的根源是使用“,”而不是“。”
由於我項目的性質,我必須在整個項目中使用文化作為“es-ES”。
如果 ”。” 被使用而不是“,”它拋出錯誤,因此能夠讓用戶使用“。” 和系統使用“,”我不得不更改代碼
protected void btnMdlEditar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal = 0;
var costo = editCosto.Text;
if (editCosto.Text.Contains("."))
{
costoDecimal = decimal.Parse(costo.ToString(), style, provider);
}
else if (editCosto.Text.Contains(","))
{
costoDecimal = decimal.Parse(costo.ToString(), style);
}
try
{
var id = CDatos.DHerramientas.UpdateHerramientas(int.Parse(txtId.Text), editCodigo.Text, editNombreCorto.Text, editDescripcion.Text, costoDecimal, editConsumible.Text);
rgHerramientas.DataBind();
rgHerramientas.Rebind();
upHerramientas.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex) { this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramientaEdit");
}
改變文化取決於“,”或“。” 被使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.