How to fix FormatException and InvalidCastException errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rkopja
    New Member
    • May 2010
    • 1

    #1

    How to fix FormatException and InvalidCastException errors

    Can anyone tell me why i get this? I just setup a Intranet and I have no problem using Firefox or even Google chrome to get into it when i use Internet Explorer i get the following error:
    Stack Trace:


    [FormatException : Input string was not in a correct format.]
    Microsoft.Visua lBasic.Compiler Services.Conver sions.ParseDoub le(String Value, NumberFormatInf o NumberFormat) +188
    Microsoft.Visua lBasic.Compiler Services.Conver sions.ToDouble( String Value, NumberFormatInf o NumberFormat) +68

    [InvalidCastExce ption: Conversion from string "" to type 'Double' is not valid.]
    Microsoft.Visua lBasic.Compiler Services.Conver sions.ToDouble( String Value, NumberFormatInf o NumberFormat) +212
    ASP.default2_as px.Page_Load(Ob ject Source, EventArgs E) +112
    System.Web.Util .CalliHelper.Ev entArgFunctionC aller(IntPtr fp, Object o, Object t, EventArgs e) +14
    System.Web.Util .CalliEventHand lerDelegateProx y.Callback(Obje ct sender, EventArgs e) +35
    System.Web.UI.C ontrol.OnLoad(E ventArgs e) +91
    System.Web.UI.C ontrol.LoadRecu rsive() +74
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +2207
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Error: InvalidCastExce ption: Conversion from string "" to type 'Double' is not valid.
    Is there such a number as "" ? No. Therefore you cannot cast "" to a number.
    Range checking is so often overlooked. One cannot assume that you always have values in the range you expect. You really should check that your strings contain something before trying to process them.

    Error: FormatException : Input string was not in a correct format.
    You tried to parse something that wasn't a number. This is a continuation of the error before it (answered above)

    Comment

    • medicinesoup
      New Member
      • May 2010
      • 5

      #3
      I don't know where in your code a string is being placed in a double variable, but try wrapping your input in a val command.

      Code:
      'VB
      Dim dblMyDouble as Double = Val("3 is my number")
      Code:
      //C#
      double dblMyDouble = Conversion.Val("3 is my number");
      Even if the string is "" or even a non-number like "hello" it will put a zero in your double variable, so if the string is something invalid, your program will still keep going with no errors.

      Comment

      Working...