我有以下代码输出数字'40':
Hashtable ht = new Hashtable();
ht.Add("numRooms", pageData.Property["romtotalt"].ToString());
string str = ht["numRooms"].ToString();
lblMigrate.Text = i.ToString();
然后我尝试将字符串转换为int,我得到一个异常/错误:
Hashtable ht = new Hashtable();
ht.Add("numRooms", pageData.Property["romtotalt"].ToString());
string str = ht["numRooms"].ToString();
int i = Convert.ToInt32(str); // <-- This is where it fails I t hink. But why??
lblMigrate.Text = i.ToString();
这是我收到的错误消息:
Server Error in '/' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7469351
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
development.templates.HotellGuide.btnMigrateHotels_Click(Object sender, EventArgs e) +956
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082
我不明白什么是错的。我之前曾多次将字符串转换为int,并且从未出现过此问题。
请帮助:)
更新
我找到了解决方案。我不知道为什么会这样......但是它有效...... 我将转换放在Try Catch中,现在它可以工作了。想出一个:op
int numRooms = 0;
int numAllergyRooms = 0;
try
{
numRooms = Convert.ToInt32(newHotel["numRooms"].ToString().Trim());
numAllergyRooms = Convert.ToInt32(newHotel["numAllergyRooms"].ToString().Trim());
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
答案 0 :(得分:6)
我认为“输入字符串格式不正确”这一行解释了这一切。在行
int i = Convert.ToInt32(str);
str可能包含字母字符。在调试时看看它,它包含什么?
答案 1 :(得分:3)
我遇到了同样的问题,但只尝试了catch方法解决了我的问题
try
{
decimal dPrice=Convert.ToDecimal(sPrice);
decimal dFreight = Convert.ToDecimal(sFreight);
decimal dLocalship = Convert.ToDecimal(sLocalship);
decimal dTarrif = Convert.ToDecimal(sTarrif);
decimal dBenefit = Convert.ToDecimal(sBenefit);
decimal dTax = Convert.ToDecimal(sTax);
decimal dVat = Convert.ToDecimal(sVat);
decimal dConv = Convert.ToDecimal(sConv);
decimal factors=(dTarrif+dBenefit+dTax+dVat)/100+1;
decimal dTotal=(dPrice+dFreight)*dConv;
dTotal=dTotal*factors;
string st = Convert.ToString(dTotal);
e.Row.Cells[iCell].Text = "price is: " + st + " Rials";
}
catch(Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
答案 2 :(得分:2)
您应该将Trim()添加到您认为失败的代码行中。这可能与过多的空间有关。
int i = Convert.ToInt32(str.Trim());
甚至可以检查 str 是否 string.Empty ,这也会导致Convert.ToInt32崩溃。
if (string.IsNullOrEmpty(str)) {
str = "0";
}
答案 3 :(得分:0)
代码没问题,但是如果str可能有空格,那么你应该在转换前删除它们:
int i = Convert.ToInt32(str.Replace(" " ,"")); // <-- This is where it fails I t hink. But why??
答案 4 :(得分:0)
您将尝试将Null或本例“(在.ToString()
之后)转换为int。
尝试插入一个可接受的占位符的内容,例如下面代码中显示的-1。
Convert.ToInt32(newHotel["numAllergyRooms"].ToString().Trim() == "" ? "-1" : newHotel["numAllergyRooms"].ToString().Trim());
答案 5 :(得分:0)
原因也可能是注册表问题。参见https://support.microsoft.com/en-us/help/942460/system-formatexception-occurs-when-attempting-to-convert-a-numeric-str
他们解释说,如果值HKEY_CURRENT_USER \ Control Panel \ International \ sPositiveSign包含错误内容,则正确的数字字符串仍可能无法转换。在我们的世界各地,此键的值应该为空(没有空间,只有空)。
可能这不是您的问题,但我在这里为可能正是由于此问题而导致问题的人添加了它...