简体   繁体   English

将字符串解析为十进制会引发 DateTime 的 FormatException

[英]Parsing string to decimal throws a FormatException for DateTime

I have a Web project which has data of different tools, Im able to insert and delete tools into a RadGrid (which is connected to a database), but Im having issues with the edit command我有一个包含不同工具数据的 Web 项目,我可以在 RadGrid(连接到数据库)中插入和删除工具,但是我在使用编辑命令时遇到问题

One of the values of the tools is "Cost" which is decimal, when Im trying to edit, half of the time it gets me this error:工具的值之一是“成本”,它是十进制的,当我尝试编辑时,有一半的时间会出现此错误:

"Input String was not in correct format error" “输入字符串格式不正确错误”

This is my Edit Method when the user clicks on the edit form modal to submit the edit当用户点击编辑表单模式提交编辑时,这是我的编辑方法

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");
       }

In the line在行

costoDecimal = decimal.Parse(costo.ToString(),style,provider); costoDecimal = decimal.Parse(costo.ToString(),style,provider);

Is where I get the error, with a FormatException poping up, mentioning an issue while parsing a string to DataTime, for some reason.是我收到错误的地方,弹出 FormatException,在将字符串解析为 DataTime 时提到一个问题,出于某种原因。 I have no idea why Im getting this error when Im using the exact code to parse decimal from string in my Insert Method我不知道为什么当我使用确切的代码从我的插入方法中的字符串解析十进制时会收到此错误

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");
       }

When I edit the data by only deleting some a part of the number or when the number is not changed it throws the error, but if I delete the number completely and enter another decimal number, it allows me to edit it当我通过仅删除数字的一部分或未更改数字来编辑数据时会引发错误,但是如果我完全删除数字并输入另一个十进制数字,则允许我对其进行编辑

7.85 to 7.89 = Error

7.85 to 6.12 = No error

Im already have我已经有了

using System.Globalization;使用 System.Globalization;

And my current culture is "es-ES"我现在的文化是“es-ES”

Edit: I tried to delete the "provider" from编辑:我试图从

costoDecimal = decimal.Parse(costo.ToString(), style, provider); costoDecimal = decimal.Parse(costo.ToString(), style, provider); which allows me to edit succesfully if I only change part of the number, but if I type a new number using "."如果我只更改数字的一部分,则允许我成功编辑,但如果我使用“。”键入新数字。 it throws me the error again它再次向我抛出错误

I think the issue is using "."我认为问题是使用“。” instead of "," which is the method used in Spain for decimals, is there a way to use both "."有没有办法同时使用“,”,而不是“,”,这是西班牙用于小数的方法。 and ","?和 ”,”?

Got it, the issue origin was using "," instead of "."明白了,问题的根源是使用“,”而不是“。”

Because of the nature of my project I have to use culture as "es-ES" for the entire project.由于我项目的性质,我必须在整个项目中使用文化作为“es-ES”。

If "."如果 ”。” was used instead of "," it threw the error, so to be able for the users to use "."被使用而不是“,”它抛出错误,因此能够让用户使用“。” and the system to use "," I had to change the code和系统使用“,”我不得不更改代码

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");
        }

Changing the culture depending if "," or "."改变文化取决于“,”或“。” was used被使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM