Search code examples
c#vb.netdynamics-crm

Conversion from string "<fetch version='1.0' ><entity na" to type 'Double' is not valid."


I am reading a string attribute from ms crm in temp string variable then i am converting it to type integer memcod so that i need to pass it as integer type to xml but it sounds strange i am not using a string or double its throws the following exception Conversion from string "<fetch version='1.0' ><entity na" to type 'Double' is not valid. Please helpexception


Solution

  • The problem is the way you tried to concatenate a bunch of strings together with an integer, memcod, by using the + operator.

    This code produces the same exception at runtime:

    Conversion from string "<foo>" to type 'Double' is not valid.

    Dim n as Integer = 0
    Dim test As String = "<foo>" +
            n +
            "</foo>"
    

    Clearly, VB sees the integer there and thinks that you're trying to do arithmetic. I guess it figures you want a double because it can't hazard a guess what else you might want. This bizarre code, for example, sets test to "11". That's a string equal to "11":

        Dim n As Integer = 0
        Dim test As String = "5" +
            n +
            "6"
    

    You can work around this issue in two ways.

    One, use VB's backwards compatible dedicated string concatenation operator instead of +:

        Dim test As String = "<foo>" &
            n &
            "</foo>"
    

    Two, explicitly stringify n:

        Dim test As String = "<foo>" +
            n.ToString() +
            "</foo>"