简体   繁体   English

输入字符串的格式不正确(CONVERTING STRING TO DECIMAL)

[英]Input String was not in correct format (CONVERTING STRING TO DECIMAL)

I am trying to convert the string from *.aspx page: 我想从* .aspx页面转换字符串:

JaveScript: JaveScript:

function updateOnClick() {

        if (!toIgnore) {
            refNo = document.getElementById("txtRef").value;
            note1000 = removeCommas(document.getElementById("txtNote_1000").value.substring(1));
            note100 = removeCommas(document.getElementById("txtNote_100").value.substring(1));
            note50 = removeCommas(document.getElementById("txtNote_50").value.substring(1));
            note20 = removeCommas(document.getElementById("txtNote_20").value.substring(1));
            note10 = removeCommas(document.getElementById("txtNote_10").value.substring(1));
            note5 = removeCommas(document.getElementById("txtNote_5").value.substring(1));
            note2 = removeCommas(document.getElementById("txtNote_2").value.substring(1));
            note1 = removeCommas(document.getElementById("txtNote_1").value.substring(1));
            coins = removeCommas(document.getElementById("txtCoins").value.substring(1));
            cheque = removeCommas(document.getElementById("txtCheque").value.substring(1));
            outstanding = removeCommas(document.getElementById("txtOutstanding").value.substring(1));
            total = removeCommas(document.getElementById("txtTotal").value.substring(1));
            collectable = removeCommas(document.getElementById("txtCollectable").value.substring(1));
            difference = removeCommas(document.getElementById("txtDifference").value.substring(1));
            collectionDate = document.getElementById(prefix + "txtDate").value;

            iniXmlHttp();

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    responseText = xmlhttp.responseText;

                    if (responseText == "") {
                        loadDailyCollectionTable();
                        document.getElementById("txtRef").focus();
                        document.getElementById("txtRef").select();
                    }

                }
            }

            xmlhttp.open("GET", "DailyCollectionPage.aspx?funcName=updateDailyCollection&RefNo=" + refNo +
            "&collectionDate=" + collectionDate + "&note1000=" + note1000 + "&note100=" + note100 +
            "&note50=" + note50 + "&note20=" + note20 + "&note10=" + note10 + "&note5=" + note5 +
            "&note2=" + note2 + "&note1=" + note1 + "&coins=" + coins + "&cheque=" + cheque +
            "&outstanding=" + outstanding + "&total=" + total + "&collectable=" + collectable + 
            "&difference=" + difference, true);

            xmlhttp.send();
        }
        else
            toIgnore = false;
    }

In Code Behind, I am getting the error in this line when I am trying to convert the string to decimal: 在Code Behind中,当我尝试将字符串转换为十进制时,我在此行中收到错误:

dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"]);

The error is: INPUT STRING WAS NOT IN A CORRECT FORMAT. 错误是: INPUT STRING WAS NOT IN A CORRECT FORMAT.

Can someone tell me what is wrong in my code. 有人能告诉我我的代码有什么问题。 Any help will be very much appreciated! 任何帮助将非常感谢!

INPUT STRING WAS NOT IN A CORRECT FORMAT. Are you sure your code always returns value it never returns NULL ? 你确定你的代码总是返回value它永远不会返回NULL吗? I think when your string is Null or Empty so you are receiving this error try to check for null or empty value first . 我认为当你的字符串为NullEmpty所以你收到此错误尝试先检查null或空值。

  if(!string.IsNullOrEmpty(Convert.Tostring(Request["note1000"]))) 
{
   dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"].ToString());

Have you tried doing this? 你试过这样做吗? - -

dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"].ToString());

also, you should use Decimal.TryParse instead, which would handle such error conditions. 另外,你应该使用Decimal.TryParse来处理这样的错误条件。

Eg: 例如:

decimal temp;
dailyCollection.Notes_1000  = Decimal.TryParse(Request["note1000"].ToString(),out temp)?temp:0.0M;

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

相关问题 输入的字符串格式不正确(从十进制到字符串) - Input string was not in a correct format (Decimal to String) 输入字符串格式不正确,从十进制到字符串? - Input string was not in a correct format from decimal to string? System.FormatException:将字符串转换为十进制时,输入字符串的格式不正确。 - System.FormatException : Input string was not in a correct format ,on converting string to decimal. 十进制类型文本框的输入字符串格式不正确 - Input string was not in a correct format for decimal type textbox 转换为整数时输入字符串格式不正确 - Input string was not in a correct format when converting to integer 输入字符串的格式不正确。将String转换为double - Input string was not in a correct format. Converting String to double 输入字符串的格式不正确。 将字符串转换为int - Input string was not in a correct format. Converting string to int 输入字符串格式不正确...将字符串转换为datetime时...错误? - input string not in correct format…when converting a string to datetime…error? 输入字符串格式不正确,将字符串转换为C#中的long? - input string was not in correct format converting string to long in c#? 输入字符串的格式不正确。 十进制类型中的预期类型 - Input string was not in a correct format. Expected type in Decimal type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM