Hi,

I have create a program for multiplication of two numbers. when i run the proram it give me an error message that [B]input string was not in correct format[/B]. But whatever data i enterd its saved in database properly.

Here is my code.

private void textBox11_TextChanged(object sender, EventArgs e)
        {
            int a, b, c;

            a = Convert.ToInt32(textBox10.Text);
            [B][/B]b = Convert.ToInt32(textBox11.Text);  // Here i got the error message.
            c = a * b;
            textBox12.Text = c.ToString();
        }

Why i got this error please help me to solve this problem....

Recommended Answers

All 10 Replies

Does textBox11 have text in it? Usually when I have this problem its because the text box is empty, and the Convert call has nothing to operate on.

You should be trapping for empty text boxes anyway, and text that isn't a number. You never know what some crazy user will do.

Just put a try-catch with the correct exception, FormatException

try
{
//Convert text boxes
}
catch (FormatException e)
{
Console.WriteLine("Invalid text in boxes!");
}

what error u get over ther post it

Ok, theory here since I don't know what input you're entering into the text boxes but...

If you're performing a Convert.ToInt32 direct from your textbox input and you're receiving an error such as what you describe I'm going to toss out the assumption that the number you're entering into the textbox is not in "Int32" format (ie: a decimal or double type instead).

Having tested your code myself it works flawlessly as long as the numbers in textBox10 and textBox11 are both integers... the moment I try to add a decimal or double value in textBox11 it gives me the error you specified.

Option 1) change your variables and conversions to double or decimal value instead of int
Option 2) stop entering non-integer values into your textboxes :twisted:

Hope that helps :) Please remember to mark solved once your issue is resolved.

Hi,

I am entering only integer numbers. But it shows me an error message.

I have a coding on textbox11 that if user enter number on textbox11 then the value stored in textbox10 is muliplyed with textbox11 and answer is shown in the textbox12.

int a,b,c;
            a = Convert.ToInt32 (textBox1.Text.ToString());
            b = Convert.ToInt32 (textBox2.Text.ToString());
            c = a * b;
            textBox3.Text = c.ToString();

try this

still its not working.

When i run it on another form it work.

Hi,

I am entering only integer numbers. But it shows me an error message.

I have a coding on textbox11 that if user enter number on textbox11 then the value stored in textbox10 is muliplyed with textbox11 and answer is shown in the textbox12.

I know what your code is doing... I copied it directly from what you put in your original post into my VS C# and created 3 textboxes to match the names in the associated form.

When I ran it it worked fine, the only time I got the error you were specifying is when I attempted to enter anything OTHER than whole number integer into one of the textboxes.

The error is indicating that the input value in your textbox is not compatible with a conversion to an integer value.

To define, an integer value is a whole number no decimal and as you haven't defined signed or unsigned can be positive or negative ranging between -2,147,483,648 and 2,147,483,647. So... unless your number entered is outside that range, the reason you are getting the error is because you are entering a non-integer (even 1.0 is considered a non-integer as far as conversion goes).

To repeat, your original code works fine, it's the numbers being put into the textboxes that is at issue. (tested repeatedly, not just speculating)

When i run it on another form it work.

The question then is, what is different between the original form you were using and the form that it's working for?

Since I know your original code snippet works as long as textBox10, textBox11 and textBox12 exist in the form and integers are entered into 10 and 11, there must be something inconsistant between your code and your form to be causing an error if you are 100% certain that the numbers you are entering are integers and not in any other format.

I'm almost wondering if in your original form you have the labelling mixed up and either textBox10 or textBox11 is actually some sort of text input in the form instead of one of the 2 numeric inputs.

Just put a try-catch with the correct exception, FormatException

try
{
//Convert text boxes
}
catch (FormatException e)
{
Console.WriteLine("Invalid text in boxes!");
}

An alternative to throwing an exception would be to utilise the TryParse method:

int a;

if (int.TryParse(textBox1.Text, out a))
{
    //success. Do work
}
else
{
    //conversion failed. Inform user
}

Throwing an exception causes a lot of overhead. Whilst invalid input is technically an error i wouldnt consider it to be truly exceptional (i always code as if my user were an unruly child...which probably isnt always far off). Using TryParse you can catch invalid input and avoid the exceptions or errors without incuring the overheads of unwinding the stack and all the other processes needed to populate the exception :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.