0

I was making a simple addition calculator in C# and I am getting a run-time error. When I press the button, I get an error at the double no1d = Double.Parse( no1 ) ; line. The error says

Exception thrown: 'System.FormatException' in mscorlib.dll Additional information: Input string was not in a correct format.

using System;
using System.Windows.Forms;

namespace Addition
{
    public partial class Addition : Form
    {
        public Addition()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string no1 = label1.Text;
            double no1d = Double.Parse( no1 )
            string no2 = label2.Text;
            double no2d = Double.Parse( no2 ) ;
            double result = no1d + no2d;
            label3.Text = result.ToString()   ;
        }
        private void label3_Click(object sender, EventArgs e)
        {
        }
    }
}

What can I do to correct the error ?

The values of label one and label two are just useless.

Edit: I found the answer now. Dimitry Bychenko was right....I had made the stupid mistake of writing 'label1' instead of 'textbox'.

IceWreck
  • 117
  • 1
  • 2
  • 10
  • 2
    what is the value of `label1`and `label2` – M.kazem Akhgary Oct 01 '15 at 07:37
  • specify your inputs; could be a culture issue (`.` instead of `,` or vice versa) – xZ6a33YaYEfmv Oct 01 '15 at 07:38
  • Simply put, you need to implement some validation: http://stackoverflow.com/questions/24279155/how-to-validate-only-number-in-winform –  Oct 01 '15 at 07:40
  • 2
    Probably you want something like `string no1 = textbox1.Text;` since it's `TextBox`, not `Label` where user inputs the value. – Dmitry Bychenko Oct 01 '15 at 07:41
  • 5
    Also, without trying to sound patronising - try and draw your own conclusions from the exceptions. It's pretty self-explanatory this one. You've got an input - and you know there's a string in there (because you've declared it so). The exception says this is in the wrong format. Format for what? Well, the line on which it's tried to format will show you that, and it'll be a result of the `Double.Parse` that's pointed out. So you can assume, your string is in the wrong format to be parsed as a double. Nobody on SO here can tell you what that exception already hasn't. –  Oct 01 '15 at 07:43
  • Dimitry Bychenko was right....I had made the stupid mistake of writing label instead of textbox. – IceWreck Oct 01 '15 at 07:58

5 Answers5

0

First of all correct your code:

private void button1_Click(object sender, EventArgs e)
{
     string no1 = label1.Text;
     double no1d = Double.Parse( no1 );
     string no2 = label2.Text;
     double no2d = Double.Parse( no2 );
     double result = no1d + no2d;
     label3.Text = result.ToString();
}

And try it once again.

metal_man
  • 580
  • 1
  • 9
  • 22
0

You need to check the input is actually a double, otherwise double.Parse will fail. Try using double.TryParse instead.

Leigh Shepperson
  • 1,043
  • 5
  • 13
0

When performing a string conversion, if no culture is passed to the method, your system's culture will be used.

In your exercice, you should then use the decimal and groups separators that fits your current culture, or pass an explicit culture to the conversion method.

For example, if your system's culture is en-EN, your numbers' format should be something like 123,456,789.01 (a wild guess).

Kilazur
  • 3,089
  • 1
  • 22
  • 48
0

Please try the following:

private void button1_Click(object sender, EventArgs e)
{
    string no1 = label1.Text;
    string no2 = label2.Text;

    if (!IsValidNumber(no1))
    {
        MessageBox.Show(String.Format("The text {0} is not a valid number", no1));
        return;
    }

    if (!IsValidNumber(no2))
    {
        MessageBox.Show(String.Format("The text {0} is not a valid number", no2));
        return;
    }

    double result = Convert.ToDouble(no1) + Convert.ToDouble(no2);
    label3.Text = result.ToString();
}

bool IsValidNumber(string textValue)
{
    try
    {
        Convert.ToDouble(textValue);
    }
    catch
    {
        return false;
    }

    return true;
}
TheDanMan
  • 1,746
  • 1
  • 17
  • 22
0

prolly the dot is the probleme if TryParse don't work.

try this :

double.Parse(no1d, CultureInfo.InvariantCulture)
Zwan
  • 632
  • 2
  • 6
  • 23