Paul
Paul

Reputation: 127

InvalidCastException was unhandled?

    Dim CustID As String = txtSrchCustID.Text
    Dim FirstName As String = txtNewCustFName.Text
    Dim SecondName As String = txtNewCustSName.Text

    If CustID And FirstName And SecondName = "" Then
        MsgBox("Please enter a term to search by")
    EndIf

This returns "Conversion from string "" to type 'Long' is not valid." I was wondering what the error is and how it's possible to fix it? The other questions that I've looked at were mostly to do with variables assigned incorrect types, but I think that isn't the issue. It occurs when all of the variables are empty.

Thanks!

Upvotes: 3

Views: 10143

Answers (1)

Arion
Arion

Reputation: 31249

What do you want to do. Do you want to check of all of them are "". Then do this:

If string.isNullOrEmpty(CustID) and _  
    string.isNullOrEmpty(FirstName) And string.isNullOrEmpty(SecondName) Then
        MsgBox("Please enter a term to search by")
    End If

Or do you want to check if one of them are "". Then do this:

If string.isNullOrEmpty(CustID) orelse _  
    string.isNullOrEmpty(FirstName) orelse string.isNullOrEmpty(SecondName) Then
        MsgBox("Please enter a term to search by")
    End If

Upvotes: 2

Related Questions