Home > Blockchain >  Error: Input string is not in the correct format
Error: Input string is not in the correct format

Time:03-07

I am new to coding so please help me out, the error appears on the commented line below:

private void order_Click(object sender, EventArgs e)

        {
            double[] itemcost = new double[2000];
            itemcost[0] = Convert.ToDouble(pep.Text) * price_pep; // Error here
            itemcost[2] = Convert.ToDouble(all.Text) * price_all;
            itemcost[3] = Convert.ToDouble(haw.Text) * price_haw;
            itemcost[4] = Convert.ToDouble(veg.Text) * price_veg;
            itemcost[5] = Convert.ToDouble(bre.Text) * price_bre;
            itemcost[6] = Convert.ToDouble(chi.Text) * price_chi;
            itemcost[7] = Convert.ToDouble(pot.Text) * price_pot;
            itemcost[8] = Convert.ToDouble(bot.Text) * price_bot;
            itemcost[9] = Convert.ToDouble(cok.Text) * price_cok;
            itemcost[10] = Convert.ToDouble(mou.Text) * price_mou;
        }

This is the error that appears:

this is the error that appears

CodePudding user response:

You have a conversion error so what this means is your input string it failed to convert to a double. You should make sure that your variable called pep is a number.

CodePudding user response:

Be careful because in coding the decimal separator needs to be a . instead of a , so for example if the user write the number "15,48" the parsing will crash because it expects "15.48", if this is the problem (you can easily check by putting a debug red dot on line 194 and when the button is pressed just hover over pep.Text to see the value stored there, if that's the problem try something like this:
double.Parse(pep.Text.Replace(",", "."));
The string.Replace() is a common method that replace a specific char or string value inside a string (in this case ",") with a different value (in this case "." to render the value parsable in double). If you want more info about this method

  • Related