Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Here I am working on a window application. I'm using access database.In this combo box I have to select a particular value and according to this value calculation have to be done. But when I am selecting the value it will be calculated perfectly and after that showing the error message.(Input string was not in a correct format.)


C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           int tot = 0;
           tot = (Convert.ToInt32(txtPay.Text) + Convert.ToInt32(txtGp.Text) + Convert.ToInt32(txtDa.Text));//This line shows the error.
           txtTotal.Text = Convert.ToString(tot);
           int a = 0;
           if (comboBox1.SelectedIndex == 0)
           {
               a =(tot * 1) / 3;
               txtNet.Text = Convert.ToString(a);
           }
           else if (comboBox1.SelectedIndex == 1)
           {
               a = (tot * 2) / 3;
               txtNet.Text = Convert.ToString(a);
           }
           else if(comboBox1.SelectedIndex==2)
           {
               a = tot;
               txtNet.Text = Convert.ToString(a);
           }

       }
Posted
Updated 13-Dec-13 19:23pm
v4
Comments
BillWoodruff 14-Dec-13 2:40am    
If you have a problem like this, always set a break-point before the error occurs, and then single-step through the code examining the values of the variables used.

One (or more) of your text boxes contains a string value that is not convertible to an int. You may either catch the exception or use the Int32.TryParse method[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Dec-13 19:41pm    
5ed :-)
—SA
[no name] 13-Dec-13 23:44pm    
+++++5
Hi
Try like this, cpallini is right..

Its better to Trim the values when you are reading from Textboxes.

C#
int tot = 0;
            int pay, gp, da;
            int.TryParse(txtPay.Text.Trim(), out pay);
            int.TryParse(txtGp.Text.Trim(), out gp);
            int.TryParse(txtDa.Text.Trim(), out pay);
            tot = pay + gp + da;
 
Share this answer
 
Comments
bigyan sahoo 15-Dec-13 8:56am    
thanks for your help
Karthik_Mahalingam 15-Dec-13 10:11am    
Welcome :)
txtTotal.Text = Tot.ToString();

just use above syntax it will work
 
Share this answer
 
try this

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int tot = 0;
            tot = (Convert.ToInt32(txtPay.Text) + Convert.ToInt32(txtGp.Text) + Convert.ToInt32(txtDa.Text));//This line shows the error.
            txtTotal.Text = tot.toString();
            int a = 0;
            if (comboBox1.SelectedIndex == 0)
            {
                a =(tot * 1) / 3;
                txtNet.Text = a.toString();
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                a = (tot * 2) / 3;
                txtNet.Text = a.toString();
            }
            else if(comboBox1.SelectedIndex==2)
            {
                a = tot;
                txtNet.Text = a.toString();
            }
 
        }
 
Share this answer
 
Comments
agent_kruger 14-Dec-13 7:26am    
he said in 2 line and you didn't even change a single letter in it.
CHill60 14-Dec-13 7:51am    
Doesn't fix the problem

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