类型'System.FormatException“的异常出现在mscorlib.dll但在用户code没有处理 [英] An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
问题描述
我试图下拉的值乘到daysRenting用户的输入值。我越来越被用于对于数据类型的格式错误。下拉菜单使用双打和daysRenting是一个int。我的问题就出在这行code ...我怎么能改写这个我的数据转换成所需要的形式?其结果将是一个双。下拉值是每日收费为选定的汽车。
保护无效button_Click(对象发件人,EventArgs的发送)
{
INT I = Convert.ToInt32(dropDown.SelectedItem.Value.ToString())* Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
}
下面是完整的code,以供参考。
<%@页面语言=C#AutoEventWireup =真codeBehind =WebForm2.aspx.cs继承=Assignment5.WebForm2%GT;<!DOCTYPE HTML>< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV> < ASP:DropDownList的ID =下拉=服务器的AutoPostBack =真OnSelectedIndexChanged =dropDown_SelectedIndexChanged>
< ASP:ListItem的值=空>选择汽车类型< / ASP:ListItem的>
< ASP:ListItem的值=29.99>体积小巧,LT; / ASP:ListItem的>
< ASP:ListItem的值=34.99将中间体< / ASP:ListItem的>
< ASP:ListItem的值=24.99>经济< / ASP:ListItem的>
< ASP:ListItem的值=44.99>标LT; / ASP:ListItem的>
< ASP:ListItem的值=49.99>全尺寸< / ASP:ListItem的>
< / ASP:DropDownList的>
< BR />< BR />
日租房:LT; ASP:文本框ID =daysRentingWIDTH =150像素=服务器< / ASP:文本框>
< BR />< BR />
< / DIV>
< ASP:按钮的ID =按钮=服务器文本=提交的OnClick =button_Click/>
< BR />< BR />
总费用:< ASP:文本框ID =TOTALCOST只读=真=服务器OnTextChanged =totalCost_TextChanged>< / ASP:文本框>
< /表及GT;
< /身体GT;
< / HTML>
和这里的的.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间Assignment5
{
公共部分类WebForm2:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ } 保护无效dropDown_SelectedIndexChanged(对象发件人,EventArgs的发送)
{ } 保护无效button_Click(对象发件人,EventArgs的发送)
{
INT I = Convert.ToInt32(dropDown.SelectedItem.Value.ToString())* Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
} 保护无效totalCost_TextChanged(对象发件人,EventArgs的发送)
{ }
}
}
您无法解析这些字符串重新的presentations在下拉双
值直接 INT
这样,因此错误。在内部,它基本上调用这个,它要求整:
Int32.Parse(29.99,NumberStyles.Integer,CultureInfo.CurrentCulture);
转换的字符串双击
代替,然后可以使用它们一样,或将其转换为一个 INT
。
双I =
Convert.ToDouble(dropDown.SelectedItem.Value)* Convert.ToInt32(daysRenting.Text);totalCost.Text = i.ToString();
I am attempting to multiply the values of dropDown to the user's inputted value in daysRenting. I am getting a format error with respect to the data types being used. dropDown uses doubles and daysRenting is an int. My problem lies in this line of code... How can I rewrite this to convert my data into the forms needed? The result will be a double. dropDown values are a daily charge for a selected car.
protected void button_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(dropDown.SelectedItem.Value.ToString()) * Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
}
Here's the full code for reference.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Assignment5.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="dropDown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dropDown_SelectedIndexChanged">
<asp:ListItem Value="null">Pick A Car Type</asp:ListItem>
<asp:ListItem Value="29.99">Compact</asp:ListItem>
<asp:ListItem Value="34.99">Intermediate</asp:ListItem>
<asp:ListItem Value="24.99">Economy</asp:ListItem>
<asp:ListItem Value="44.99">Standard</asp:ListItem>
<asp:ListItem Value="49.99">Full Size</asp:ListItem>
</asp:DropDownList>
<br/><br/>
Days Renting:<asp:TextBox ID="daysRenting" width="150px" runat="server" </asp:TextBox>
<br /><br/>
</div>
<asp:Button ID="button" runat="server" Text="Submit" OnClick="button_Click" />
<br/><br/>
Total Cost:<asp:TextBox ID="totalCost" ReadOnly="true" runat="server" OnTextChanged="totalCost_TextChanged"></asp:TextBox>
</form>
</body>
</html>
and here's the .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assignment5
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void dropDown_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void button_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(dropDown.SelectedItem.Value.ToString()) * Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
}
protected void totalCost_TextChanged(object sender, EventArgs e)
{
}
}
}
You can't parse those string representations of double
values in the drop-down directly to int
like that, hence the error. Internally, it's basically calling this, and it expects an integer:
Int32.Parse("29.99", NumberStyles.Integer, CultureInfo.CurrentCulture);
Convert the strings to double
instead, then either use them like that or cast them to an int
.
double i =
Convert.ToDouble(dropDown.SelectedItem.Value) * Convert.ToInt32(daysRenting.Text);
totalCost.Text = i.ToString();
这篇关于类型'System.FormatException“的异常出现在mscorlib.dll但在用户code没有处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!