简体   繁体   English

异常详细信息:System.FormatException:输入字符串的格式不正确。 解析不起作用

[英]Exception Details: System.FormatException: Input string was not in a correct format. Parse not working

I am getting this error "Exception Details: System.FormatException: Input string was not in a correct format." 我收到此错误“异常详细信息:System.FormatException:输入字符串的格式不正确。” It seems my Convert and Parse is not working. 看来我的转换和解析不起作用。 Im using ASP.NET C#. 我正在使用ASP.NET C#。 Also is there a way to just read numeral values instead of using textbox? 还有没有办法只读取数字值而不使用文本框?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="Payroll" runat="server">
<div>

    <h1>Exception Painting</h1>
    <h2>Payroll Calculator</h2>

    <p>Employee Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
    <p>Pay Rate: <asp:TextBox ID="PayRate" runat="server"></asp:TextBox>
    <p>Salary Employee: <asp:TextBox ID="SalaryEmployee" runat="server"></asp:TextBox>
    <p>Hours Worked: <asp:TextBox ID="HoursWorked" runat="server"></asp:TextBox>

     <asp:Button ID="Submit" runat="server" Text="Submit" />

</div>
</form>

<%
    double overtimeHours = 0;
    double overtimePay = 0;
    double regularPay = 0;
    double totalPay = 0;
    double regularHours = 0;
    double basePay = 0;
    double hrsWrked = 0;
    double payRt = 0;

    string nm = Name.Text;

    hrsWrked = Convert.ToDouble(HoursWorked.Text);
    //I cant figure out what im doing wrong here..
    hrsWrked = double.Parse(HoursWorked.Text);

    string slry = Name.Text;

    payRt = Convert.ToDouble(PayRate.Text);
    payRt = double.Parse(PayRate.Text);

    double overtimePayRt = payRt * 1.5;

    if (Page.IsPostBack)
    {
        Payroll.Visible = false;

        if (SalaryEmployee.Equals("no") && hrsWrked > 40)
            overtimeHours = hrsWrked - 40;
            regularHours = hrsWrked - overtimeHours;
            overtimePay = overtimeHours * overtimePayRt;
            regularPay = payRt * regularHours;
            totalPay = overtimePay + regularPay;
            basePay = hrsWrked * 52 * totalPay;
            Response.Write("Employee Name: " + nm + "Base Salary: " + basePay + "Hours Worked: " + hrsWrked + "Total Weekly Pay: " + totalPay);

        if (SalaryEmployee.Equals("yes"))
            regularHours = 40;
            totalPay = payRt * regularHours;
            basePay = hrsWrked * 52 * totalPay;
            Response.Write("Employee Name: " + nm + "Base Salary: " + basePay + "Hours Worked: "           + hrsWrked + "Total Weekly Pay: " + totalPay);
    }
    %>

</body>
</html>

you are parsing and converting uninitialized values 您正在解析和转换未初始化的值

<p>Employee Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<p>Pay Rate: <asp:TextBox ID="PayRate" runat="server">0.0</asp:TextBox>
<p>Salary Employee: <asp:TextBox ID="SalaryEmployee" runat="server">0.0</asp:TextBox>
<p>Hours Worked: <asp:TextBox ID="HoursWorked" runat="server">0.0</asp:TextBox>

this will get your code to compile but you will need to be checking these values every time the user changes them cause it will break as soon as you have values that won't parse or convert correctly 这将使您的代码得以编译,但是每次用户更改它们时,您都需要检查这些值,因为一旦您具有无法正确解析或正确转换的值,它将立即中断

here is an example of one way to catch errors for this. 这是捕获错误的一种方法示例。

bool bueno = true;//spanish for good




try
{
    payRt = Convert.ToDouble(PayRate.Text);
    payRt = double.Parse(PayRate.Text);
    //I cant figure out what im doing wrong here..
    hrsWrked = double.Parse(HoursWorked.Text);
}
catch (System.FormatException fEX)
{
    bueno = false;
    Response.Write(fEX.Message);//you could do more here 
}
catch (System.OverflowException ofEX)
{
    bueno = false;
    Response.Write(ofEX.Message);//you could do more here 
}
catch (System.ArgumentException aEX)
{
    bueno = false;
    Response.Write(aEX.Message);//you could do more here 
}

//I would do more about these errors but this is an example


if(bueno)
{
    double overtimePayRt = payRt * 1.5;

    if (Page.IsPostBack)
    {

暂无
暂无

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

相关问题 Int32.Parse(“356882”) 生成异常 System.FormatException: '输入字符串的格式不正确。' - Int32.Parse(“356882”) generate exception System.FormatException: 'Input string was not in a correct format.' System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException: &#39;输入字符串的格式不正确。&#39; - System.FormatException: 'Input string was not in a correct format.' System.FormatException: &#39;输入字符串的格式不正确。&#39; 数据网格 - System.FormatException: 'Input string was not in a correct format.' data grid System.FormatException:&#39;输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.' System.FormatException: '输入字符串的格式不正确。' WinForms - System.FormatException: 'Input string was not in a correct format.' WinForms “System.FormatException:输入字符串的格式不正确。” - "System.FormatException: Input string was not in a correct format." Linq 错误输入字符串的格式不正确。 System.Exception {System.FormatException} - Linq Error Input string was not in a correct format. System.Exception {System.FormatException} uint.Parse 对有效字符串抛出 System.FormatException:“输入字符串的格式不正确。” - uint.Parse on a valid string throws System.FormatException: 'Input string was not in a correct format.' double.parse System.FormatException: &#39;输入字符串的格式不正确。&#39; - double.parse System.FormatException: 'Input string was not in a correct format.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM