5

Exception thrown: System.FormatException in mscorlib.dll

{"Input string was not in a correct format."}

I have been at this all day. Please help! Those above were the error messages btw. I am a bit new to C# so I could use a bit more detailed answers (I don't get the jargon so well)

public partial class Form1 : Form
{
    double a, b, d, f, g, t;
    string z;
    int c;


    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, System.EventArgs e)
    {

    }



    private void button7_Click(object sender, EventArgs e)
    {
        a = Convert.ToDouble(textBox18.Text);
        z = Convert.ToString(comboBox1);
        d = Convert.ToDouble(checkBox1);
        f = Convert.ToDouble(textBox13.Text);
        g = Convert.ToDouble(textBox14.Text);
        t = Convert.ToDouble(textBox19.Text);


        if (z == "a")
        {
            textBox15.Text = Convert.ToString(a * g * d * t);
        }
        else if (z == "b")
        {
            textBox15.Text = Convert.ToString(c);
        }
    }
Jason Kats
  • 61
  • 1
  • 1
  • 5
  • 1
    One of your textbox#.Text might be null or empty – eakgul Dec 26 '16 at 06:20
  • Possible duplicate of [System.FormatException : Input string was not in a correct format ,on converting string to decimal.](http://stackoverflow.com/questions/23130554/system-formatexception-input-string-was-not-in-a-correct-format-on-converting) – Satpal Dec 26 '16 at 06:22
  • from combo box if u need to fetch value do this comboBox1.SelectedValue.ToString() , see for checkbox too . they hav properties so use that to fetch value. – FakeisMe Dec 26 '16 at 06:23

3 Answers3

2

These could be the possible issues,

the given input may not be in those format which can be converted to double

a = Convert.ToDouble(textBox18.Text);
    z = Convert.ToString(comboBox1.SelectedValue);
    d = Convert.ToDouble(checkBox1.Checked);
    f = Convert.ToDouble(textBox13.Text);
    g = Convert.ToDouble(textBox14.Text);
    t = Convert.ToDouble(textBox19.Text);

or may be it should be something like

    z = Convert.ToString(comboBox1.SelectedValue);
   d = Convert.ToDouble(checkBox1.Checked);
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

You need to take values from Controls instead of parsing the control itself,

like --> for combobox it should be comboBox.SelectedValue

Karthik AMR
  • 1,694
  • 21
  • 29
0

The problem is with this line :

  textBox15.Text = Convert.ToString(a * g * d * t); 

because you are multiplying the d with other values and d contain different type of value as checkbox1 does not return right value

So first you should get the value of check box in right format like :

d= Convert.ToDouble(comboBox.SelectedValue)
Anurag_Soni
  • 542
  • 2
  • 17
  • you can find more detail about error in this link :http://stackoverflow.com/questions/12269254/how-to-resolve-input-string-was-not-in-a-correct-format-error – Anurag_Soni Dec 26 '16 at 06:28