开发者

Double ParseDouble - Input string was not in a correct format - How to Fix?

I keep receiving this same error about 3-4 times a day and have had no luck tracking down how to resolve this issue. According to the stack trace, the error is occurring within the Microsoft.VisualBasic library so how exactly would I go about fixing this issue?

Here's the report I send to myself and what gets generated by the server. Any suggestions would be greatly appreciated.

REQUEST
=======
HttpContext.Current.Request.RawUrl: /
UserHostAddress: 207.70.41.78
UserHostName: 207.70.41.78
Request.Browser.Type: IE8
Request.Browser.Platform: Win2000


DATA
====
System.Collections.ListDictionaryInternal


MESSAGE
=======
Input string was not in a correct format.

SOURCE
=====
Microsoft.VisualBasic

TARGET SITE
===========
Double ParseDouble(System.String, System.Globalization.NumberFormatInfo)

STACK TRACE
============
   at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger开发者_运维技巧(String Value)

Update:

I understand what the error is and what would cause the error to occur. The problem I'm running into is finding out where exactly this error is coming from within code. The stack trace tells me its coming from Visual Basic but there's nothing else. No line numbers, no other info about what function caused this error to occur, there's zilch to go on so I'm left scratching my head trying to diagnose something, that looks to be wrapped up in a .dll probably.

We're running Ektron 7.6.6 sp2 if that sheds any light onto the subject.

Is there any way to get a more robust stack trace? I checked the event viewer on the server and the info i posted earlier is all that's displayed in the event viewer. Which is none to helpful.

I appreciate all of your responses to my question.


This error means some code like this is being executed Double.ParseDouble("bacon"). bacon is delicious, but it isn't a double. Put a try/catch around the code to see what is being passed in to ParseDouble. This should help determine what is causing the problem (probably not bacon) and that should help determine where it is coming from. Is a user entering it in a textbox? Is it coming from the database? From there you can change the ParseDouble to Double.TryParse. This will let you get the double if it is one and if not provide an alternate branch of code to execute when bad data gets input (at which point you can return an error like "Hey, Eat that bacon, don't put it in there").


Not a lot to go on here, but the error message IS telling you what's wrong - you are trying to parse a string into a double that has non-numeric characters in it. Look further up your stack trace to see where the parse method is being called and it might point you in the right direction.

I've seen this on our maintennace website when people type in a $ or other punctuation when entering dollar amounts. You might need to filter that sort of thing, but first find out where it is coming from.


I have no idea where exactly you should be looking (is this a truncated error log?) but I'd audit for uses of Double.Parse and replace them with Double.TryParse.

The conversion may not succeed, but it will be a manageable failure if you've been fed garbage input.


please provide a valid input parameter which can be converted to double. And use Double.TryParse instead of Parse.


I ran into this problem working with some terrible legacy code. I searched high and low for any unprotected parsing and found none. I had made what I thought was a simple change:

From

link.HRef = Globals.BaseURL + Globals.URLRecipe + "?Key=" + Context.Server.UrlEncode(strTemp)

To

link.HRef = Globals.BaseURL & Globals.URLRecipe & "?RecipeId=" & RecipeId

Now, it had already be throwing the Double Parse error, plus another error related to strTemp being a string with a recipe key. When the first URL was followed, it would have a Parse error, because it was trying to convert the Key to the RecipeId, which is an int. After pulling the actual ID into the method, I changed the URL.

Suddenly, I was getting this error:

System.FormatException: Input string was not in a correct format.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)

After much head banging, I dug into my ancient knowledge of VB operators and remembered some subtleties between '+' and '&' for string concatenation.

The '+' operator that was in use previously was trying to take my shiny new Int RecipeId and add it to the rest of the URL, which the compiler was trying to parse as a Double under the hood.

Changing it to this solved it:

link.HRef = Globals.BaseURL & Globals.URLRecipe & "?RecipeId=" & RecipeId


I had similar problem when i was calling friend function from another form (without importing form itself). For some reason VB compiler doesn't show full stack trace log (or at least the exception message doesn't say to look into inner exception). So, after like ~2 hours of fiddling i have found that passing exception into custom exception handler, that reads inner exception, will, in fact, show actual exception and stacktrace as inner exception. Here is my exception code handler snippet (just in case).

 Public Function ExceptionToString(ByVal Message As String, ByVal ex As Exception) As String
        Dim Splitter As String = "-------------------------------------------------------------------"
        Dim ex0 As String = Message & vbCrLf & Splitter & vbCrLf
        Dim ex1 As String = ""
        Dim ex2 As String = ""
        Dim ex3 As String = ""
        Dim ex4 As String = ""
        Dim ex5 As String = ""
        Try
            ex1 = ex.Message & vbCrLf & Splitter & vbCrLf
        Catch exc1 As Exception
        End Try
        Try
            ex2 = ex.StackTrace & vbCrLf & Splitter & vbCrLf
        Catch exc1 As Exception
        End Try
        Try
            ex3 = ex.InnerException.Message & vbCrLf & Splitter & vbCrLf
        Catch exc1 As Exception
        End Try
        Try
            ex4 = ex.InnerException.StackTrace & vbCrLf & Splitter & vbCrLf
        Catch exc1 As Exception
        End Try
        Try
            Dim DataStr As String = ""
            For i As Integer = 0 To ex.Data.Keys.Count
                Try
                    DataStr &= ex.Data.Item(ex.Data.Keys(i)) & vbCrLf
                Catch exc2 As Exception
                End Try
            Next
            ex5 = DataStr & vbCrLf & Splitter & vbCrLf
        Catch exc1 As Exception
        End Try

        Dim Result As String = ex0 & ex1 & ex2 & ex3 & ex4 & ex5

        Return Result

    End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜