Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This should be simple, but for the life of me I can't figure it out.
Private Sub cboCustomer_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboCustomer.SelectedValueChanged
        If cboCustomer.SelectedIndex >= 0 Then
            lngID = CLng(cboCustomer.SelectedItem)
            Debug.Print("lngID = " & CStr(lngID))
        End If
    End Sub


There is only one value in the customers combobox and that is the only customer ID of 1

I get this exception
InvalidCastException was unhandled by user code
and I don't know why this exception is being thrown. All I am doing is assigning the "SelectedValue" from the combobox into a lngID so I can run a query and return the proper results.

Can someone tell me why this is happening and what I need to do to rectify the problem

What I have tried:

If I knew why it is happening I might be able to rectify the problem.
Posted
Updated 3-Apr-18 6:08am

1 solution

You are selecting a SelectedItem, which is an object that you cannot convert to a long.
VB
lngID = CLng(cboCustomer.SelectedItem)
Do you want the index of that item? Then use:
VB
lngID = cboCustomer.SelectedIndex
More on this here: ComboBox.SelectedItem Property[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900