VB.NET 2005 Cast Error

Error: Conversion from string “0” to type ‘Integer’ is not valid

During a recent rollout at my company we would encounter the error message above on some PCs. The PCs were running either Windows 2000 or Windows XP with the latest service packs installed. I believed the error was coming from the Microsoft.VisualBasic.dll.

I found a workaround online which is included here:

Modify the registry key

HKEY_CURRENT_USERControl PanelInternationalsPositiveSign

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.

Changing this setting is per user, so you may have to apply this change for every user of the PC.

The code were the error occurs was reading a value from a database and casting it to an integer. Here is the VB code:

Dim drcInfo As DataRowCollection
Dim intFieldValue As Integer = 0

‘…drcInfo filled with data (missing)…

For Each drDataRow As DataRow In drcInfo               
intFieldValue = CInt(drDataRow.Item(0).ToString)
      Exit For

This is a known bug and there is a Microsoft knowledge base article here:

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.