title: Not able to cast string to int. Error msg: Input string was not in a correct format
body: <p>I havethe following code which outputs the number '40':</p>
  
  <pre><code>Hashtable ht = new Hashtable();
  ht.Add("numRooms", pageData.Property["romtotalt"].ToString());
  string str = ht["numRooms"].ToString();
  lblMigrate.Text = i.ToString();
  </code></pre>
  
  <p>Then I try to convert the string to an int, and I get an exception / error:</p>
  
  <pre><code>Hashtable ht = new Hashtable();
  ht.Add("numRooms", pageData.Property["romtotalt"].ToString());
  string str = ht["numRooms"].ToString();
  int i = Convert.ToInt32(str);  // &lt;-- This is where it fails I t hink. But why??
  lblMigrate.Text = i.ToString();
  </code></pre>
  
  <p>This is the error message I get:</p>
  
  <pre><code>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&amp; 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 
  </code></pre>
  
  <p>I don't understand what's wrong. I'va castet string to int multiple times before, and never has this problem occured.</p>
  
  <p>Please help :)</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have found a solution. I have NO idea why this works.... but it works...<br />
  I put the convertion inside a Try Catch, and now it works. Figure that one out :op</p>
  
  <pre><code>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);
  }
  </code></pre>