Click here to Skip to main content
15,902,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to calculte sum of five textboxes in 1 text box.... but when ever i remain empty one text box out of five textboxes it is giving an error Input string was not in the correct format below is my code

C#
if (textBox6.Text.Length == 0)

        // textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        {
            textBox11.Text = (Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox7.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox8.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox9.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox10.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();

        }
        else
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
Posted

use Int32.TryParse[^] for all textbox values and finally get the sum. sample code:
C#
int val1, val2;
int.TryParse(textBox1.Text,out val1);
int.TryParse(textBox2.Text,out val2);
int sum =  val1 + val2;

for check input for empty, you better use one of following methods
String.IsNullOrEmpty Method[^]
String.IsNullOrWhiteSpace Method[^]
but even not empty input could give format exception because of input value does not consist of an optional sign followed by a sequence of digits (0 through 9). you better use TryParse and it will not give any exception even you have non digit characters. additionally you can use return value of Int.TryParse to inform user about incorrect input format.
 
Share this answer
 
v6
Comments
Member 10690757 9-Jul-14 2:41am    
but it is giving an error like int is does not exixt in current context?
DamithSL 9-Jul-14 2:46am    
use simple i in int.TryParse or use Int32.TryParse
Member 10690757 9-Jul-14 2:50am    
ok
Member 10690757 9-Jul-14 3:03am    
thankyou It is working :)
Look at your code:
C#
if (textBox6.Text.Length == 0)
        {
            textBox11.Text = (Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox7.Text.Length == 0)

So why do you think that if "textBox6" has something (gawd knows what) in it, then "textBox7", and "textBox8", and 9, and 10, and 5, and 11 will all have valid numbers in them?

Don't do it like that.
Use int.TryParse to convert each and every text box to a number first, and report any problems back to the user. Then do your checks and use the data!

And BTW two other things:
1) Don't check text that way: use string.IsNullOrWhitespace instead - it means that a space in a text box doesn't count!
2) Please, stop using the default Visual Studio names for things! Using "proper" names may take you 20 seconds longer when you create the textbox or button but it saves you ages later on because it is obvious what the textbox is meant to hold, and you code becomes - at least a little - self documenting.
 
Share this answer
 

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