'Error: Input string is not in the correct format
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:

Solution 1:[1]
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.
Solution 2:[2]
This error is caused because the input type cannot be converted to double type, I make a similar code to reproduce the issue, here is my test result?

We can add try catch to solve this problem, such as:
private void order_Click(object sender, EventArgs e)
{
try
{
double[] itemcost = new double[2000];
itemcost[0] = Convert.ToDouble(pep.Text) * price_pep;
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;
}
catch
{
MessageBox.Show("Please enter the correct number");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego_San |
| Solution 2 | Jingmiao Xu-MSFT |
