Help with search on a Datatable.Select

I am trying to search for a productname (SearchParam) ordered alphabetically,or if pass "[UID]" will search based on that and return it alphabetically (by the name of the product).
 I Get the Error "Syntax error: Missing operand after 'A' operator. "


 Public Function SortData(ByRef dt As DataTable, Optional ByVal colnames As String = "", Optional ByVal SearchParam As String = "") As DataSet
        Dim dt_temp As DataTable
        Dim dr As DataRow

        If dt.Rows.Count > 1 Then
            ' make a copy, whack the original
            dt_temp = dt.Copy
            dt.Clear()

            ' sort and import the data back
            If SearchParam = "" Then
                For Each dr In dt_temp.Select("[Name] like '*'", "Name Asc")
                    dt.ImportRow(dr)

                Next
            Else
                'actually do a search!
                Dim strSQL As String
                If colnames = "[NAME]" Then
                    strSQL = "'" & colnames & " like %'" & SearchParam & "%'" & "," & SearchParam & " Asc" & "'%"
                    For Each dr In dt_temp.Select(strSQL)
                        dt.ImportRow(dr)

                    Next
                End If
                '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                '|||||||||||||||||||||||||||||||||||||||||||
                'Change this: LIKE '%"+MyTextBox.Text+"%'";
                'To THis: LIKE "%'" + MyTextBox.Text + "'%";
                '|||||||||||||||||||||||||||||||||||||||||||
                'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv






                If colnames = "[UID]" Then
                    strSQL = "'" & colnames & "=" & SearchParam & "," & SearchParam & " Asc" & "'"
                    'For Each dr In dt_temp.Select(colnames & "=" & SearchParam, SearchParam & " Asc")
                    For Each dr In dt_temp.Select(strSQL)
                        dt.ImportRow(dr)

                    Next
                End If

            End If

            Dim ds As New DataSet
            dt_temp = dt

            ds.Tables.Add(dt.Copy)
            dt = Nothing
            dt_temp = Nothing
            Return ds
        End If


    End Function
peterdidowAsked:
Who is Participating?
 
appariConnect With a Mentor Commented:
try changing the else part as follows

'actually do a search!

                Dim strSQL As String
                If colnames = "[NAME]" Then
                    strSQL = " " & colnames & " like '%" & SearchParam & "%'"
                    For Each dr In dt_temp.Select(strSQL, colNames & " Asc " )
                        dt.ImportRow(dr)

                    Next
                End If
                '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                '|||||||||||||||||||||||||||||||||||||||||||
                'Change this: LIKE '%"+MyTextBox.Text+"%'";
                'To THis: LIKE "%'" + MyTextBox.Text + "'%";
                '|||||||||||||||||||||||||||||||||||||||||||
                'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv






                If colnames = "[UID]" Then
                    strSQL = " " & colnames & "='" & SearchParam & "'"
                    'For Each dr In dt_temp.Select(colnames & "=" & SearchParam, SearchParam & " Asc")
                    For Each dr In dt_temp.Select(strSQL, colNames & " Asc " )
                        dt.ImportRow(dr)

                    Next
                End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.