Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SFry - 07 Feb 2007 01:15 GMT
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
Patrick Steele - 07 Feb 2007 03:18 GMT
> Hi all,
>
[quoted text clipped - 29 lines]
>    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...)

Signature

Patrick Steele
http://weblogs.asp.net/psteele

SFry - 07 Feb 2007 18:45 GMT
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.  

> > Hi all,
> >
[quoted text clipped - 33 lines]
> settings thing.  Are all of your regional settings the same (Number
> format, decimal character, thousands separator, etc...)
RobinS - 07 Feb 2007 06:32 GMT
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.
-----------------------------
> Hi all,
>
[quoted text clipped - 42 lines]
>
> Steve
SFry - 07 Feb 2007 18:59 GMT
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.  

> 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
[quoted text clipped - 54 lines]
> >
> > Steve
RobinS - 09 Feb 2007 07:21 GMT
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.
--------------------------------------------
>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
[quoted text clipped - 81 lines]
>> >
>> > Steve
SFry - 09 Feb 2007 00:22 GMT
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.


> Hi all,
>
[quoted text clipped - 33 lines]
>
> Steve

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.