Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
i'm trying to read the text box fields values and checking if they are null assigning the zero integer value.

then i'm assigning the everytextbox.text to integer values. but here it is showing error as Input string was not in a correct format. how to overcome this

<pre lang="c#">if (txtYearofPurchased.Text == null || txtMonthofPurchased.Text == null || txtCurrentMonthEnd.Text == null || txtCurrentYearEnd.Text == null||

         txtCost.Text == null || txtDepnRate1.Text ==null)
     {
         txtYearofPurchased.Text = Convert.ToString(0);
         txtMonthofPurchased.Text = Convert.ToString(0);
         txtCurrentMonthEnd.Text= Convert.ToString(0);
         txtCost.Text= Convert.ToString(0);
         txtDepnRate1.Text = Convert.ToString(0);

     }

     int yearofpurchase = Int32.Parse(txtYearofPurchased.Text);
     int monthofpurchase = Int32.Parse(txtMonthofPurchased.Text);
     int CurrentMonth = Int32.Parse(txtCurrentMonthEnd.Text);// present month
     int CurrentYearend = Int32.Parse(txtCurrentYearEnd.Text);// present year
     float Cost = Int32.Parse(txtCost.Text);
     float DepnRate1 = Int32.Parse(txtDepnRate1.Text)
Posted
Comments
[no name] 27-Apr-15 5:18am    
which area u got that error ? put a break point then check it.

Try this

use TryParse instead of Parse

C#
int yearofpurchase=0, monthofpurchase=0, CurrentMonth=0, CurrentYearend=0;
           float Cost=0, DepnRate1=0;

           int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
           int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
           int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
           int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);// present month
           int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);// present year
           int.TryParse(txtCost.Text, out Cost);
           int.TryParse(txtDepnRate1.Text, out DepnRate1);
 
Share this answer
 
You seem to have a typo in the last two lines. Try replacing them with the following:

C#
float Cost = float.Parse(txtCost.Text);
float DepnRate1 = float.Parse(txtDepnRate1.Text)


If you use Int32.Parse() on strings which contain floating point numbers (that is, they have a decimal point), you will get an argument exception.
 
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