Home All Groups Group Topic Archive Search About

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

Author
7 Feb 2007 1:15 AM
SFry
Hi all,

I'm looking for a little help in determining what's causing this exception. 
I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).  It
doesn't appear to matter under what context the conversion takes place -
whether it's implicit or explicitly cast - it barfs either way.  The source
of the string "0" doesn't appear to matter either...whether it's from a text
box, data grid, or simply coded directly, it throws the exception every time.
The following will fail, for example (option strict: off)...

            Dim s As String = "0"
            Dim i As Integer

            i = s

The problem is specific to my machine and 1 other colleague's...it works
fine for everybody else.  Furthermore, running a VB.NET app developed in VS
2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
re-installing the 2.0 framework, but still get the exception.  Full text of
the exception from the above code follows...

System.InvalidCastException: Conversion from string "0" to type 'Integer' is
not valid. ---> 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.ToInteger(String
Value)
   --- End of inner exception stack trace ---
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
   at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36

Any help would be much appreciated!  Thanks so much!

Steve

Author
7 Feb 2007 3:18 AM
Patrick Steele
In article <EDC49825-D80E-477B-9A93-A28489C67***@microsoft.com>,
S***@discussions.microsoft.com says...
Show quote
> Hi all,
>
> I'm looking for a little help in determining what's causing this exception. 
> I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).  It
> doesn't appear to matter under what context the conversion takes place -
> whether it's implicit or explicitly cast - it barfs either way.  The source
> of the string "0" doesn't appear to matter either...whether it's from a text
> box, data grid, or simply coded directly, it throws the exception every time.
>  The following will fail, for example (option strict: off)...
>
>             Dim s As String = "0"
>             Dim i As Integer
>
>             i = s
>
> The problem is specific to my machine and 1 other colleague's...it works
> fine for everybody else.  Furthermore, running a VB.NET app developed in VS
> 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
> re-installing the 2.0 framework, but still get the exception.  Full text of
> the exception from the above code follows...
>
> System.InvalidCastException: Conversion from string "0" to type 'Integer' is
> not valid. ---> 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.ToInteger(String
> Value)
>    --- End of inner exception stack trace ---
>    at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
> Value)
>    at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
> C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36

It worked for me too (with Option Strict Off).  Could be a regional
settings thing.  Are all of your regional settings the same (Number
format, decimal character, thousands separator, etc...)

Author
7 Feb 2007 6:45 PM
SFry
Thanks for your response Patrick!  My regional settings are identical to
those of another colleague's who doesn't receive the exception, and there's
no setting that indicates a 0 should not be treated as an integer.  I would
also think that the absence of the exception in VS 2003 would imply that the
cause isn't an OS regional setting. 

Show quote
"Patrick Steele" wrote:

> In article <EDC49825-D80E-477B-9A93-A28489C67***@microsoft.com>,
> S***@discussions.microsoft.com says...
> > Hi all,
> >
> > I'm looking for a little help in determining what's causing this exception. 
> > I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).  It
> > doesn't appear to matter under what context the conversion takes place -
> > whether it's implicit or explicitly cast - it barfs either way.  The source
> > of the string "0" doesn't appear to matter either...whether it's from a text
> > box, data grid, or simply coded directly, it throws the exception every time.
> >  The following will fail, for example (option strict: off)...
> >
> >             Dim s As String = "0"
> >             Dim i As Integer
> >
> >             i = s
> >
> > The problem is specific to my machine and 1 other colleague's...it works
> > fine for everybody else.  Furthermore, running a VB.NET app developed in VS
> > 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
> > re-installing the 2.0 framework, but still get the exception.  Full text of
> > the exception from the above code follows...
> >
> > System.InvalidCastException: Conversion from string "0" to type 'Integer' is
> > not valid. ---> 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.ToInteger(String
> > Value)
> >    --- End of inner exception stack trace ---
> >    at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
> > Value)
> >    at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
> > C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36
>
> It worked for me too (with Option Strict Off).  Could be a regional
> settings thing.  Are all of your regional settings the same (Number
> format, decimal character, thousands separator, etc...)
>
> --
> Patrick Steele
> http://weblogs.asp.net/psteele
>
Author
7 Feb 2007 6:32 AM
RobinS
This is the worst programming method ever. Why in the world would you even
do that? Do you have Option Strict On in your VB projects? This wouldn't
compile if you did. If you have some need to convert a string to an
integer, do it specifically. Code like this causes weird problems that is
really difficult to track down.

  Dim s as String = "0"
  Dim i as Integer
  i = CType(s, Integer)

Robin S.
-----------------------------
Show quote
"SFry" <S***@discussions.microsoft.com> wrote in message
news:EDC49825-D80E-477B-9A93-A28489C67973@microsoft.com...
> Hi all,
>
> I'm looking for a little help in determining what's causing this
> exception.
> I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).
> It
> doesn't appear to matter under what context the conversion takes place -
> whether it's implicit or explicitly cast - it barfs either way.  The
> source
> of the string "0" doesn't appear to matter either...whether it's from a
> text
> box, data grid, or simply coded directly, it throws the exception every
> time.
> The following will fail, for example (option strict: off)...
>
>            Dim s As String = "0"
>            Dim i As Integer
>
>            i = s
>
> The problem is specific to my machine and 1 other colleague's...it works
> fine for everybody else.  Furthermore, running a VB.NET app developed in
> VS
> 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
> re-installing the 2.0 framework, but still get the exception.  Full text
> of
> the exception from the above code follows...
>
> System.InvalidCastException: Conversion from string "0" to type 'Integer'
> is
> not valid. ---> 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.ToInteger(String
> Value)
>   --- End of inner exception stack trace ---
>   at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
> Value)
>   at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
> C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36
>
> Any help would be much appreciated!  Thanks so much!
>
> Steve
>
>
>
Author
7 Feb 2007 6:59 PM
SFry
I appreciate your response RobinS, but apparently you didn't read my post
very thoroughly.  I specified that I have Option Strict Off and that this
exception is thrown regardless of the context of the conversion, whether it
be implicit or explicitly cast.  Explicitly converting the string as you have
indicated below will still throw the exception on my machine.  The problem
was identified when I was trying to convert the text in a text box into an
integer.  The simple code I provided is merely an example of something that
should work (with option strict off) that doesn't. 

Show quote
"RobinS" wrote:

> This is the worst programming method ever. Why in the world would you even
> do that? Do you have Option Strict On in your VB projects? This wouldn't
> compile if you did. If you have some need to convert a string to an
> integer, do it specifically. Code like this causes weird problems that is
> really difficult to track down.
>
>   Dim s as String = "0"
>   Dim i as Integer
>   i = CType(s, Integer)
>
> Robin S.
> -----------------------------
> "SFry" <S***@discussions.microsoft.com> wrote in message
> news:EDC49825-D80E-477B-9A93-A28489C67973@microsoft.com...
> > Hi all,
> >
> > I'm looking for a little help in determining what's causing this
> > exception.
> > I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).
> > It
> > doesn't appear to matter under what context the conversion takes place -
> > whether it's implicit or explicitly cast - it barfs either way.  The
> > source
> > of the string "0" doesn't appear to matter either...whether it's from a
> > text
> > box, data grid, or simply coded directly, it throws the exception every
> > time.
> > The following will fail, for example (option strict: off)...
> >
> >            Dim s As String = "0"
> >            Dim i As Integer
> >
> >            i = s
> >
> > The problem is specific to my machine and 1 other colleague's...it works
> > fine for everybody else.  Furthermore, running a VB.NET app developed in
> > VS
> > 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
> > re-installing the 2.0 framework, but still get the exception.  Full text
> > of
> > the exception from the above code follows...
> >
> > System.InvalidCastException: Conversion from string "0" to type 'Integer'
> > is
> > not valid. ---> 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.ToInteger(String
> > Value)
> >   --- End of inner exception stack trace ---
> >   at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
> > Value)
> >   at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
> > C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36
> >
> > Any help would be much appreciated!  Thanks so much!
> >
> > Steve
> >
> >
> >
>
>
>
Author
9 Feb 2007 7:21 AM
RobinS
Yes, I did get that. My point was, "Why do you care, since this is not good
programming practice and you shouldn't be doing it this way anyway?"   ;-)

Robin S.
--------------------------------------------
Show quote
"SFry" <S***@discussions.microsoft.com> wrote in message
news:BF4DAAA2-DD72-44A7-B23D-6A93DE6F44DB@microsoft.com...
>I appreciate your response RobinS, but apparently you didn't read my post
> very thoroughly.  I specified that I have Option Strict Off and that this
> exception is thrown regardless of the context of the conversion, whether
> it
> be implicit or explicitly cast.  Explicitly converting the string as you
> have
> indicated below will still throw the exception on my machine.  The
> problem
> was identified when I was trying to convert the text in a text box into
> an
> integer.  The simple code I provided is merely an example of something
> that
> should work (with option strict off) that doesn't.
>
> "RobinS" wrote:
>
>> This is the worst programming method ever. Why in the world would you
>> even
>> do that? Do you have Option Strict On in your VB projects? This wouldn't
>> compile if you did. If you have some need to convert a string to an
>> integer, do it specifically. Code like this causes weird problems that
>> is
>> really difficult to track down.
>>
>>   Dim s as String = "0"
>>   Dim i as Integer
>>   i = CType(s, Integer)
>>
>> Robin S.
>> -----------------------------
>> "SFry" <S***@discussions.microsoft.com> wrote in message
>> news:EDC49825-D80E-477B-9A93-A28489C67973@microsoft.com...
>> > Hi all,
>> >
>> > I'm looking for a little help in determining what's causing this
>> > exception.
>> > I'm getting it when running VB.NET apps developed in VS 2005 (.NET
>> > 2.0).
>> > It
>> > doesn't appear to matter under what context the conversion takes
>> > place -
>> > whether it's implicit or explicitly cast - it barfs either way.  The
>> > source
>> > of the string "0" doesn't appear to matter either...whether it's from
>> > a
>> > text
>> > box, data grid, or simply coded directly, it throws the exception
>> > every
>> > time.
>> > The following will fail, for example (option strict: off)...
>> >
>> >            Dim s As String = "0"
>> >            Dim i As Integer
>> >
>> >            i = s
>> >
>> > The problem is specific to my machine and 1 other colleague's...it
>> > works
>> > fine for everybody else.  Furthermore, running a VB.NET app developed
>> > in
>> > VS
>> > 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
>> > re-installing the 2.0 framework, but still get the exception.  Full
>> > text
>> > of
>> > the exception from the above code follows...
>> >
>> > System.InvalidCastException: Conversion from string "0" to type
>> > 'Integer'
>> > is
>> > not valid. ---> 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.ToInteger(String
>> > Value)
>> >   --- End of inner exception stack trace ---
>> >   at
>> > Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
>> > Value)
>> >   at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
>> > C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36
>> >
>> > Any help would be much appreciated!  Thanks so much!
>> >
>> > Steve
>> >
>> >
>> >
>>
>>
>>
Author
9 Feb 2007 12:22 AM
SFry
I discovered the solution to this should anyone else run into it...

Modify the HKEY_CURRENT_USER\Control Panel\International\sPositiveSign
registry key so that its value is nothing.  If it appears as though the value
is already empty (as it did in my case), edit the data string to some
arbitrary viewable character, save it, open it up again and remove the value.


Show quote
"SFry" wrote:

> Hi all,
>
> I'm looking for a little help in determining what's causing this exception. 
> I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0).  It
> doesn't appear to matter under what context the conversion takes place -
> whether it's implicit or explicitly cast - it barfs either way.  The source
> of the string "0" doesn't appear to matter either...whether it's from a text
> box, data grid, or simply coded directly, it throws the exception every time.
>  The following will fail, for example (option strict: off)...
>
>             Dim s As String = "0"
>             Dim i As Integer
>
>             i = s
>
> The problem is specific to my machine and 1 other colleague's...it works
> fine for everybody else.  Furthermore, running a VB.NET app developed in VS
> 2003 (.NET 1.1) from my machine does NOT produce the error.  I tried
> re-installing the 2.0 framework, but still get the exception.  Full text of
> the exception from the above code follows...
>
> System.InvalidCastException: Conversion from string "0" to type 'Integer' is
> not valid. ---> 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.ToInteger(String
> Value)
>    --- End of inner exception stack trace ---
>    at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
> Value)
>    at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
> C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36
>
> Any help would be much appreciated!  Thanks so much!
>
> Steve
>
>
>

AddThis Social Bookmark Button