An unhandled exception of type "System.FormatException" occurred in the mscorlib.dll file

Each time I click the "Calculate" button, I get the following message: An unhandled exception of type "System.FormatException exception" occurred in the mscorlib.dll file. Additional information: the input line was not in the correct format.

I have to display a message when salary input is below $ 9.75.

He then highlights this line of code:

if (Convert.ToInt32(RateTextBox.Text) < 9.75m) 

Here is the Calculate Button method (I'm sure I made a few mistakes):

  private void CalcButton_Click(object sender, EventArgs e) { // The "Calculate" button calculates gross pay, taxes, and net pay and then displays name, department, gross pay, taxes, and net pay using currency format for various amounts in the rich text box // Gross pay= (hours * rate) // Taxes= (25% of gross pay) // Net pay (gross pay ?taxes) //calculate Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text); Taxes = TAX * Gross_pay; Net_Pay = Gross_pay - Taxes; annual_salary = Net_Pay; //display DisplayOutPut.Text = ""; DisplayOutPut.Text += NameTextBox.Text + "\n"; DisplayOutPut.Text += "Hours:" + HoursTextBox.Text + "\n"; DisplayOutPut.Text += "Rate:" + RateTextBox.Text + "\n"; DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n"; // Hours*Rate DisplayOutPut.Text += "Taxes:" + Taxes.ToString("C") + "\n"; DisplayOutPut.Text += "Net Pay:" + Net_Pay.ToString("C"); //handling the invalid inputs if (NameTextBox.Text == "") { MessageBox.Show("Name is missing.", "Error"); } if (Convert.ToInt32(HoursTextBox.Text) >= 70) { MessageBox.Show("Please Enter a Valid hour.", "Invalid data type."); } if (RateTextBox.Text == "" && (RateTextBox.Text == ",")) { MessageBox.Show("Please Enter a valid amount.", "Invalid data type ($)"); } if (Convert.ToInt32(HoursTextBox.Text) >= 70) { MessageBox.Show("You have exceeded the maximum hours per week."); } else if (Convert.ToInt32(HoursTextBox.Text) < 10) { MessageBox.Show("You cannot input less than 10 hours."); } if (Convert.ToInt32(RateTextBox.Text) < 9.75m) { MessageBox.Show("Please enter the minimum wage."); } //overtime pay if (Convert.ToInt32(HoursTextBox.Text) >= 41) { Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) * 1.5m; DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n"; } //Medical/Dental and 401k deductions...as well as tax collected. if (MedicalDentalDeductions.Checked) { Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 50.00m; } if(FourOneKDeduction.Checked) { Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 0.05m * 100; } if ((MedicalDentalDeductions.Checked) && (FourOneKDeduction.Checked)) { Taxes = TAX * Gross_pay; } DisplayOutPut.Text= "Medical/Dental deduction:" + Taxes +"401k deduction:"+ Taxes; } 
+7
debugging c # unhandled-exception
source share
2 answers

You convert a number to an integer. So, if you entered (say) 9.5, that will not work, because it is not an integer.

You will almost certainly use decimal.TryParse , noting that the return value will tell if the parsing is successful or not.

 decimal userRate; if (!decimal.TryParse(RateTextBox.Text, out userRate)) { // Indicate to the user that the input is invalid, and return from // the method } // Now use userRate 
+3
source share

You will get this error if you type something that cannot be converted to an integer since you used Convert.ToInt32 . Try:

 if (Convert.ToDecimal(RateTextBox.Text) < 9.75m) 

Or, even better, switch to decimal.TryParse , which allows you to easily handle invalid input without handling exceptions.

+1
source share

All Articles