0

I'm having "Input string was not in a correct format." error from this code

rainTb.Text = " " + int.Parse(parsed[0]) * 100 / 1023;

There's no error in my code. Except for the error above. I've made a calculation to extract the analogue value (above calculation) from Arduino hardware via serial communication (cable). Arduino works fine though. Everything was fine though until this error shows up... Why? cries

p/s : I've gone through many solutions but still.. So here I am now. Sorry for my English

private void ProcessCOMRx(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(COMRx))
        {
            systemTb.Text = "Processing Data...";
            systemTb.BackColor = System.Drawing.Color.Lime;
            string [] parsed = COMRx.Split(',');
            int curveNo;

            if (parsed.Count() > zedAll.GraphPane.CurveList.Count())
               curveNo = zedAll.GraphPane.CurveList.Count();                
            else                
                curveNo = parsed.Count();                

            for (int k = 0; k < curveNo; k++)
            {
                for (int j = zedAll.GraphPane.CurveList[k].NPts - 1; j > 0; j--)
                {
                    zedAll.GraphPane.CurveList[k].Points[j].Y = zedAll.GraphPane.CurveList[k].Points[j - 1].Y;
                }
                double temp = 0;                    
                try
                {
                    temp = double.Parse(parsed[k]);
                }                    
                catch                    
                {
                    systemTb.Text = "Parse Error";
                    systemTb.BackColor = System.Drawing.Color.Red;
                }
                rainTb.Text = "" + int.Parse(parsed[0]) * 100 / 1023;
                phTb.Text = "" + (3.5 + int.Parse(parsed[1]) * 4.5 / 1023);
                moistTb.Text = "" + int.Parse(parsed[2]) * 100 / 1023;
                tempTb.Text = "" + int.Parse(parsed[3]) * 100 / 1023;


                zedAll.GraphPane.CurveList[k].Points[0].X = 0;
                zedAll.GraphPane.CurveList[k].Points[0].Y = temp;                  
            }

        }
        else
        {                
            this.BeginInvoke(new EventHandler(processPumpStates));
        }

    }
Aqilah Zainuddin
  • 13
  • 1
  • 2
  • 7

2 Answers2

1

There are few possible cases why it happens. One is because (1) parsed[0] number is too big, another is because parsed[0] contains (2) non-number, (3) non-recognized thousand separators, or (4) decimal separator (which should not exist in int parsing) in the applied culture for your code.

In all cases, please check the value of parsed[0] with your Visual Studio debugger and make sure that it has purely-acceptable numerical format for int range. Something like:

1234

Also, you may consider of

  1. using TryParse instead of Parse to ensure that the non-parsed number does not cause you exception problem.
  2. use ToString() for printing your numerical calculation result,
  3. check the result of TryParse and
  4. beware of the integer division that you potentially do in the original int.Parse(parsed[0]) * 100 / 1023:

Something like this:

 int val;
 bool result = int.TryParse(parsed[0], out val);
 if (!result)
     return; //something has gone wrong

 Textbox1.Text = " " + (val * 100d / 1023d).ToString(); //note the d
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
0

You are getting such error on the basics of the value contained in the parsed[0] if it is a convertible string your code works fine else it throws the exception that you got. ie, the content inside the parsed[0] is not convertible to an integer, in such situations you should Use int.TryParse instead for int.Parse, it is having internal error handling, which help you to determine whether the conversion is successful or not.

So the code will looks like:

int tempNumber=0;
if (int.TryParse(parsed[0], out tempNumber))
{
    Textbox1.Text = " " + (tempNumber * 100 / 1023).ToString();
}
else 
{
    Textbox1.Text= "invalid zipCode";
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88