无法将string转换为int。 错误信息:inputstring的格式不正确
我有下面的代码输出数字“40”:
Hashtable ht = new Hashtable(); ht.Add("numRooms", pageData.Property["romtotalt"].ToString()); string str = ht["numRooms"].ToString(); lblMigrate.Text = i.ToString();
然后我尝试将string转换为一个int,并得到一个exception/错误:
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 int串,并且从来没有发生过这个问题。
请帮忙 :)
更新
我find了一个解决scheme。 我不知道为什么这个工程….但它的作品…
我把转换里面的Try Catch,现在它的工作。 图一出:操作
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); }
我认为“inputstring格式不正确”一行解释了这一切。 在线
int i = Convert.ToInt32(str);
str可能包含字母字符。 在debugging的时候看一看,它包含了什么?
我有同样的问题,但只有尝试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); }
您应该将Trim()添加到您认为失败的代码行中。 这可能与多余的空间有关。
int i = Convert.ToInt32(str.Trim());
也许甚至检查是否str是string.Empty ,这也会导致Convert.ToInt32崩溃。
if (string.IsNullOrEmpty(str)) { str = "0"; }
代码是好的,但如果str可能有空格,所以你应该在转换之前删除它们:
int i = Convert.ToInt32(str.Replace(" " ,"")); // <-- This is where it fails I t hink. But why??
这将是你正在试图将Nulls,或在这种情况下“(” .ToString()
)转换为一个int。
尝试一些将为下面的代码中显示的空值插入可接受的占位符,例如-1。
Convert.ToInt32(newHotel["numAllergyRooms"].ToString().Trim() == "" ? "-1" : newHotel["numAllergyRooms"].ToString().Trim());