Conversion from string "0" to type 'Integer' is not valid.

I'm getting the above error when trying to pass the value from a hiddenfield control to an integer parameter in a class.

This results in a "Input string was not in a correct format. " error.

If I try to cast the hiddenfield's value to an integer, I get the same conversion error ('Conversion from string "0" to type 'Integer' is not valid.').

is this a bug



Answer this question

Conversion from string "0" to type 'Integer' is not valid.

  • Faisal H. Siddiqui

    I had this error not long ago!  Surprisingly, it had to do with the computer's culture setting.
     in english, we use 1.33, french is 1,33   so if the variable isn't formatted correctly, the conversion fails).


    Try adding this line to the start of your code.  It's sets the culture setting to a default, which will  tell the compiler to process intergers, singles, doubles... etc differently.



    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture

     


    Dustin.


  • sheetalsonar

    interestingly, when I copied your three lines of code into my app, both of the conversion lines failed.

    I tried a third conversion method: ctype(s, integer) which also failed.

    as for my code:
    '**********************************************************
    Public Function getpubsWithOptionalParams(ByVal lTblSiteID As Integer) As Data.DataSet

    'code removed for brevity

    End Function
    '**********************************************************

    code snippet from aspx file

    <asp:ObjectDataSource ID="odsSite" runat="server" SelectMethod="getpubsWithOptionalParams" TypeName="dataAccess">

    <SelectParameters>
    <asp:ControlParameter ControlID="hdnSiteCode" DefaultValue="0" Name="lTblSiteID" PropertyName="Value" Type="Int32" />
    </SelectParameters>
    </asp:ObjectDataSource>


     


  • DrunkenMunk

    Basically I'm having exactly the same problems with dates. If I force the output format to be US using a format specigier then it can parse my date fine on the "post".

    There seem to be a few people suffering from this too.

    I am going to try and raise a support call with microsoft as it looks like a bug with the framework.


  • Graham Rhodes

    Could you post the code you're using

    There are several possibilities to convert strings to integers:


    Dim s As String = "0"
    Dim i As Integer = Convert.ToInt32(s)
    Dim j As Integer = Integer.Parse(s)

     


  • Conversion from string "0" to type 'Integer' is not valid.