[英]Int32.Parse(“356882”) generate exception System.FormatException: 'Input string was not in a correct format.'
[英]Exception Details: System.FormatException: Input string was not in a correct format. Parse not working
我收到此错误“异常详细信息:System.FormatException:输入字符串的格式不正确。” 看来我的转换和解析不起作用。 我正在使用ASP.NET C#。 还有没有办法只读取数字值而不使用文本框?
<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>
您正在解析和转换未初始化的值
<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>
这将使您的代码得以编译,但是每次用户更改它们时,您都需要检查这些值,因为一旦您具有无法正确解析或正确转换的值,它将立即中断
这是捕获错误的一种方法示例。
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.