Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to multiply numericupdown value with textbox value and show it in another textbox. when i enter value in textbox and after that select value from numericupdown, then all goes well. but when i do vice-versa, it gives me error.

error: Input string was not in a correct format.

my code is

C#
public void Multiply()
        {
            int price = Convert.ToInt32(textBox3.Text);
            int quantity = Convert.ToInt32(numericUpDown1.Value);

            int total = quantity * price;
            textBox1.Text = total.ToString();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            Multiply();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            Multiply();
        }




please help
Posted
Updated 15-Feb-14 0:12am
v2

1 solution

Simple: if there is no value in your TextBox when you change the value in the NumericUpDown, then the conversion from a string will fail.
Try this instead:
C#
public void Multiply()
    {
    int price;
    if (int.TryParse(textBox3.Text, out price))
        {
        int quantity = (int)numericUpDown1.Value;
        int total = quantity * price;
        textBox1.Text = total.ToString();
        }
    }
 
Share this answer
 
Comments
Member 9764132 15-Feb-14 6:21am    
thanks OriginalGriff, it worked.
OriginalGriff 15-Feb-14 6:29am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900