0

I am trying to convert the string from *.aspx page:

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:

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

The error is: 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!

4
  • Did you tried checking what you get in Request["note1000"] by simply converting it to string? may be the value you are getting is not decimal value, or possibly you might be getting a value that is something like garbage characters appended with decimal values. First check the value that you get. then you can decide for further steps Commented Aug 26, 2013 at 9:12
  • 1
    Whatz this removeCommas seems to be a culprit, also post the value available for note1000 Commented Aug 26, 2013 at 9:15
  • What is the value in Request["note1000"]? Commented Aug 26, 2013 at 9:36
  • 1
    @V4Vendetta, Nice catch man! I used that function to remove commas, dollar signs, & other characters and pass the values as decimal. If the users key in 100 and press enter, the textbox will show $100.00. So removeCommas() function will remove those characters and return the decimal value. However, if the users enter 100 and straightaway click on the update button, that function has nothing to remove and return error as a wrong input format. Thanks for pointing out my problem. Commented Aug 27, 2013 at 4:02

2 Answers 2

2

INPUT STRING WAS NOT IN A CORRECT FORMAT. Are you sure your code always returns value it never returns 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 .

  if(!string.IsNullOrEmpty(Convert.Tostring(Request["note1000"]))) 
{
   dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"].ToString());
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for your nice answer. But the error giving me the problem is because of JavaScript function, removeCommas, that I used inside the script.
0

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.

Eg:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.