Click here to Skip to main content
15,891,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code for an application where total amount will be calculated each time when the value of textbox controls changes .I had set the text property of all textbox to '0'
and under the event i write this code
C#
public void addaction()
       {
           int total = int.Parse(txtsilabordercharges.Text) + int.Parse(txtbattacharges.Text) + int.Parse(txtdriverpayment.Text.ToString()) + int.Parse(txtrent.Text.ToString()) + int.Parse(txtextra.Text.ToString());
          int amount = total - int.Parse(txtdiscount.Text);
          txttotal.Text = total.ToString();
          txtamount.Text = amount.ToString();

       }

       private void txtsilabordercharges_TextChanged(object sender, EventArgs e)
       {
           addaction();
       }

but iam getting exception at

int total = int.Parse(txtsilabordercharges.Text) + int.Parse(txtbattacharges.Text) + int.Parse(txtdriverpayment.Text.ToString()) + int.Parse(txtrent.Text.ToString()) + int.Parse(txtextra.Text.ToString());

Input string was not in a correct format.>> can anyone pls help to sort the mistake
Posted

Int32.Parse[^] will convert the passed String[^] to an integer[^]. However, it is required that the String absolutely can be converted to an integer!
For example, "Hello" or "" could never be converted to an integer. "42" or "0" can be converted.
You should try Int32.TryParse[^] instead.
Usage:
C#
int battaCharges = 0;
if (int.TryParse(txtbattacharges.Text, battaCharges))
{
   // Conversion succeeded, battaCharges now has a numeric value.
}
else
{
   // Conversion failed, battaCharges is still 0.
   // You might want to give the user a message that the specified text was not numeric.
}
What I like to do in these cases is Inherit the TextBox and make it only accept numeric values[^]. In this scenerio the user will never have to be prompted with annoying MessageBoxes telling them their input was incorrect.
 
Share this answer
 
v3
Comments
OriginalGriff 21-Feb-12 14:07pm    
I think you are getting confused between languages - TryCast is a VB keyword... :laugh:
Sander Rossel 21-Feb-12 14:25pm    
Ughh... Thanks for the heads up! I meant to say TryParse of course :D
Mohammad A Rahman 21-Feb-12 16:31pm    
Good answer :)
Sander Rossel 21-Feb-12 17:18pm    
Thanks :)
The first thing that shows up is: you do not need to use ToString on Text properties - they are already strings, so no conversion can usefully happen.

If you are not suer which one is causing the problem, then either break it up into chunks so you can see which part is wrong:
C#
int silabordercharges = int.Parse(txtsilabordercharges.Text);
int battacharges = int.Parse(txtbattacharges.Text);
int driverpayment = int.Parse(txtdriverpayment.Text);
int rent = int.Parse(txtrent.Text);
int extra = int.Parse(txtextra.Text);
int total = silabordercharges + battacharges + driverpayment + rent + extra;

Or put a break point on the line, and look at each of elements in turn.
 
Share this answer
 
Comments
Sander Rossel 21-Feb-12 15:21pm    
Calling ToString on each String is indeed silly. Looking at each element one step at a time may help finding the problem, but will it solve it? When it comes to user input and parsing I always strongly suggest using TryParse (not TryCast :) ). It's the only way me thinks.
+5 for some good suggestions.
OriginalGriff 21-Feb-12 15:37pm    
I was going to edit mine (I posted it before realizing I'd forgotten to mention TryParse) - but then I say yours as decided not to! :laugh:
You are calling the method addaction() on textchange event so i think when you enter something and after that you remove it text box becomes empty so its text can not be converted to int so try doing like this
C#
private void txtsilabordercharges_TextChanged(object sender, EventArgs e)
       {if(txtsilabordercharges.Text!="")
           addaction();
       }
 
Share this answer
 
I have a suggestion
Since all your text boxes shall accept only numbers, I think it is better to use
masked TextBox.
As you are calling addaction() in txtsilabordercharges_TextChanged the totalling will be done only if the text changes in txtsilabordercharges_TextChanged. But when text changes in other text boxes, then also you need to calculate total. Hence, either use a button to calculate the total, or bind the TextChanged event of all the TextBoxes to the same event handler as below

C#
txtsilabordercharges.TextChanged += TextBox_TextChanged;
txtbattacharges.TextChanged += TextBox_TextChanged;
txtdriverpayment.TextChanged += TextBox_TextChanged;
txtrent.TextChanged += TextBox_TextChanged;
txtextra.TextChanged += TextBox_TextChanged;
txtdiscount.TextChanged += TextBox_TextChanged;


private void TextBox_TextChanged(object sender, EventArgs e)
{if (!string.IsNullOrEmpty((sender as TextBox).Text)))
   addaction();
}
 
Share this answer
 
v2

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