I am creating a form that will filter datagridview while typing in a textbox. I came through this error and I would like to know why is occur.

Dim dt As DataTable = Ds_Pos.Employees
        Dim dv As New DataView(dt)
        'findcrit is declared as string public and it contain a field name stored in combo
        Dim _RowFilter As String = ""
        Dim _FieldType = dt.Columns(findcrit).DataType.ToString

        Select Case _FieldType
            Case "System.Int32" or "System.Int64"
                _RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox10.Text & "'"
           Case "System.Double"
                _RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox10.Text & "'"
            Case "System.String"
                _RowFilter = findcrit & " like '" & TextBox10.Text & "*'"
        End Select
        dv.RowFilter = _RowFilter
        EmployeesDataGridView.DataSource = dv

I got error in line 8
Conversion from string "System.Int32" to type 'Long' is not valid.

Now If i change the above Select Case to

Select Case _FieldType
            Case "System.Int32"
                _RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox10.Text & "'"
            Case "System.Int64"
                _RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox10.Text & "'"
            Case "System.Double"
                _RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox10.Text & "'"
            Case "System.String"
                _RowFilter = findcrit & " like '" & TextBox10.Text & "*'"
        End Select

It works fine

I know the Select Case can accept the or but why not in the above code?

Replace or with a comma

Case "System.Int32", "System.Int64"

Also it is not good programming practice to dimension variables without explicitly stating the datatype.

Change "Dim _FieldType =" to "Dim FieldType As String =".

Also you should always put a "Case Else" in your Select Case statement; even if there is no coding under it. Otherwise an error will occur if your search criteria isnt found in any of the other Case statements.

Replace or with a comma

Case "System.Int32", "System.Int64"

Hi Tom

I had take the or with me from VFP I guess :)

I had test example and it did not give right result neither a error message

Dim A = "1"
Select Case A
Case "1" or "2"
    MsgBox ("1 or 2")
Case "3" or "4"
    MsgBox ("3" or "4")
End Select

but when I replace it with

Dim A = "1r"
Select Case A
Case "1r" or "2r"
MsgBox ("1r or 2r")
Case "3" or "4"
MsgBox ("3r" or "4r")
End Select

I gives the error message. so I guess I did not test deeply.

Thank you.

Also it is not good programming practice to dimension variables without explicitly stating the datatype.

Change "Dim _FieldType =" to "Dim FieldType As String =".

I had tested that before, and it gives the same effect in all aspect.

dim x = "1"
x 'just type x and wait the intellisense to showup it will tell you that x is declared as string

Also you should always put a "Case Else" in your Select Case statement; even if there is no coding under it. Otherwise an error will occur if your search criteria isnt found in any of the other Case statements.

Good Idea.

Thank you for your input.

dim x = "1"
x 'just type x and wait the intellisense to showup it will tell you that x is declared as string

Thats funny because everything I said works fine here but your code apparently is not... Your choice, you can be lazy and save typing two whole words and spend hours asking for help because of it or you can learn to program correctly. Not turning on "Option Strict" & "Option Explicit" is more laziness in an attempt to have the program overlook errors.

01) Turn on Option Strict & Explicit
02) Explicitly declare your variable datatypes
03) Use prefixes in your controls & variable names
so that you know at a glance what datatype
there supposed to be.
04) If your having trouble with a "Select Case" statement
try looking in the help file at "Select Case"

Dim strMySearchValue As String = "1r"

        Select Case strMySearchValue
            Case "1r", "2r"
                MessageBox.Show("Case 1r or 2r")
            Case "3r", "44"
                MessageBox.Show("Case 3r or 4r")
            Case Else
                MessageBox.Show("strA is something else")
        End Select

Thats funny because everything I said works fine here but your code apparently is not

What that suppose to mean?

ou can be lazy and save typing two whole words and spend hours asking for help because of it or you can learn to program correctly

lazy has nothing to do in here, it is a fact.
1- Dim x = "A"
2- Dim x as String
x = "A"
3- Dim x as String = "A"

Are Identical 100% regarding the debugging and execution.

If you are referring to

Dim x = "1"
Debug.Print (x+1)

will give 2 if Option Strict is off, that is another matter.

03) Use prefixes in your controls & variable names

I had asked this question before, 3/4 MVP advice against it, althought I was using it extensively in VFP, since in VFP this is allowed.

Local I as Integer
I = "Samir"

But I got the advice not to use any kind of prefexing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.