简体   繁体   English

mscorlib.dll中发生了未处理的“System.FormatException”类型异常

[英]An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Every time I hit the calculate button I receive the following message: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. 每次我点击计算按钮时,我都会收到以下消息: mscorlib.dll中出现未处理的“System.FormatException”类型的异常附加信息:输入字符串的格式不正确。

I'm supposed to display a message when the wage input is below $9.75. 当工资输入低于9.75美元时,我应该显示一条消息。

It then highlights this line of code: 然后它突出显示这行代码:

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

Here is the Calculate Button method (I'm sure I've made more than one error): 这是Calculate Button方法(我确定我犯了多个错误):

 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;

    }

You're converting a number to an integer . 您正在将数字转换为整数 So if you entered (say) 9.5 that would fail, because it's not an integer. 所以,如果你输入(比如说)9.5会失败,因为它不是一个整数。

You should almost certainly be using decimal.TryParse , noting that the return value will say whether parsing succeeded or not. 您几乎肯定会使用decimal.TryParse ,注意返回值将说明解析是否成功。

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

You will get that error if you type something that's not convertible to an integer , since you used Convert.ToInt32 . 如果输入的内容不能转换为整数 ,则会出现错误,因为您使用的是Convert.ToInt32 Try: 尝试:

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

Or, better yet, switch to using decimal.TryParse , which will allow you to easily handle improper input without exception handling. 或者,更好的是,切换到使用decimal.TryParse ,这将允许您轻松处理不正确的输入而无需异常处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll “ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常” - “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常其他信息:输入字符串的格式不正确 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format 尝试解析DateTime时-mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - When trying to parse DateTime - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 在C#中将十进制转换为十六进制,并出现错误“ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常” - decimal to hexadecimal in c# with mistake “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” mscorlib.dll中发生“ System.FormatException”类型的未处理异常,被卡住 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll, stuck mscorlib.dll 中发生了类型为“System.FormatException”的未处理异常 谁能帮我解决这个错误? - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Can anyone help me with this error? c# 中的运行时错误 - mscorlib.dll 中发生类型为“System.FormatException”的未处理异常 - Run-time error in c# - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生类型&#39;System.FormatException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in the user code mscorlib.dll 中出现“System.FormatException”类型的异常,但未在用户代码中处理 - An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM