[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

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
0
peterdidow
Asked:
peterdidow
1 Solution
 
appariCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now