none
Input String was not in a correct format RRS feed

  • Question

  • Hi All!

    I am trying to make a calculator in Windows Form Application using C# in visual studio 2010..well i have 3 text boxes 2 for input and 1 for getting output..  i just want the sqrt of the no. i enter in 1st text box and want to add restriction on 2nd text box.. but it gives the error: Input string was not in a correct format on the line of if condition...plz help me...........:((

    try
                {
                    double a, result;

                    if (textBox2.Text!=" ")
                     {

                                  MessageBox.Show("Function only give the sqrt of 1st No", "Error");
                                  textBox1.Clear();
                                  textBox2.Clear();
                                  textBox3.Clear();
                                  textBox1.Focus();
                     }
                    a = Convert.ToInt16(textBox1.Text);
                    result = Math.Sqrt(a);
                    textBox3.Text = " " + result;
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }

                                        
    Wednesday, July 18, 2012 2:39 PM

Answers

  • Try changing your code like below,

    try
    {
        double a, result;
     
        if (textBox2.Text != String.Empty)
        {
           MessageBox.Show("Function only give the sqrt of 1st No", "Error");
           textBox2.Clear();
           textBox3.Clear();
           textBox1.Focus();
        }
        
        double.TryParse(textBox1.Text.Trim(), out a);
        result = Math.Sqrt(a);
        textBox3.Text = " " + result;
    }
    catch (Exception e1)
    {
         MessageBox.Show(e1.Message);
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:13 PM
    • Unmarked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:24 PM
    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:48 PM
    Wednesday, July 18, 2012 5:17 PM
  • yeah thanx alot Mr. Adavesh..it also works...:)

    but the main problem was because of not adding the else block..i have solved this..now the correct code is given below..

    try
                {
                    double a,result;

                    if (textBox2.Text != "")
                    {

                        MessageBox.Show("Function only give the sqrt of 1st No", "Error");
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        textBox1.Focus();
                    }
                    else
                    {
                        a = Convert.ToInt16(textBox1.Text);
                        result = Math.Sqrt(a);
                        textBox3.Text = " " + result;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }

    thanx alot to all of you...:)

    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:37 PM
    Wednesday, July 18, 2012 6:10 PM

All replies

  • What did you enter to TextBox1 and TextBox2?
    Wednesday, July 18, 2012 2:45 PM
  • the integer values for example 25 or 4

    if i don't add the if condition then it gives correct output for the value entered in textbox1 but give exception error for the value entered in textbox2 va

    Wednesday, July 18, 2012 2:52 PM
  • Debug your code and you will see, what happens:

    If you enter anything else but a space to TextBox2, the 'if' statement will be true and all the textboxes will be cleared after that you try to convert the empty string to a number, that's why you got the exception.

    Wednesday, July 18, 2012 3:04 PM
  • Try changing your code like below,

    try
    {
        double a, result;
     
        if (textBox2.Text != String.Empty)
        {
           MessageBox.Show("Function only give the sqrt of 1st No", "Error");
           textBox2.Clear();
           textBox3.Clear();
           textBox1.Focus();
        }
        
        double.TryParse(textBox1.Text.Trim(), out a);
        result = Math.Sqrt(a);
        textBox3.Text = " " + result;
    }
    catch (Exception e1)
    {
         MessageBox.Show(e1.Message);
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:13 PM
    • Unmarked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:24 PM
    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:48 PM
    Wednesday, July 18, 2012 5:17 PM
  • yeah thanx alot Mr. Adavesh..it also works...:)

    but the main problem was because of not adding the else block..i have solved this..now the correct code is given below..

    try
                {
                    double a,result;

                    if (textBox2.Text != "")
                    {

                        MessageBox.Show("Function only give the sqrt of 1st No", "Error");
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        textBox1.Focus();
                    }
                    else
                    {
                        a = Convert.ToInt16(textBox1.Text);
                        result = Math.Sqrt(a);
                        textBox3.Text = " " + result;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }

    thanx alot to all of you...:)

    • Marked as answer by MadihaMalik1 Wednesday, July 18, 2012 6:37 PM
    Wednesday, July 18, 2012 6:10 PM