Invalid Cast

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
At the following code line it is giving me an invalid cast exception not sure why. Keeping in mind that option strict is on. Any given would be greatly appreiciated.

Dim tindex as integer

tindex = Convert.ToInt32(ComboBox4.SelectedValue)
 
Its going to depend a lot on where your code is actually executing.

You might try
Code:
If (Me.ComboBox4.SelectedIndex <> -1) Then
   Dim tindex as Integer = CInt(Me.ComboBox4.SelectedItem.ToString())
End If
 
SelectedIndex

The selectedindex value is 0. Because it loads the DisplayMembers of the Combobox on the initial load.
 
Is there a SelectedValue property? Or are you referring to SelectedItem? Like PlausiblyDamp said, check the value of SelectedItem (or SelectedItem.ToString). Could be that it is nothing or an invalid string. Check either that (.SelectedIndex <> -1) or that (Not .SelectedItem Is Nothing).
 
Back
Top