0

I've been attempting to create a dynamic search function that utilises multiple User Controls (strips) to build the search conditions. The strings are being built properly, so no problem there. However when the search() function is called in the Module, the flowLayoutPanel no longer recognises that there are strips inside it (I've put message boxes in to track where they are lost). Any suggestions would be appreciated.

**************frmAttributesSearch Class************************************************

Public Class frmAttributesSearch

    Private Sub btnAddCondition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewCondition.Click
        FlowLayoutPanel1.Controls.Add(New UCAddAttribute)
        MsgBox("Controls are here when added: " & FlowLayoutPanel1.Controls.Count)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        MsgBox("Still here on search click: " & FlowLayoutPanel1.Controls.Count)
        dgPlayers.DataSource = search()
    End Sub

    Public Function buildFilterString()
        MsgBox("Still lost on buildFilterString: " & FlowLayoutPanel1.Controls.Count)
        Dim myString As String = ""

        For Each strip As UCAddAttribute In FlowLayoutPanel1.Controls
            myString = myString + strip.cmbAttribute.SelectedItem & " " & strip.cmbEqualityFactor.SelectedItem & " " & Integer.Parse(strip.txtNumber.Text) & " AND "
        Next

        myString = myString + "1 = 1"

        Return myString

    End Function

End Class

**************Module*****************************************************************

Public Function search() As DataTable

        MsgBox("Lost here on search: " & frmAttributesSearch.FlowLayoutPanel1.Controls.Count)

Dim dt As New DataTable
        Dim Str As String = _
       <String> SELECT 
                    *
                FROM 
                    Player
                INNER JOIN Report ON Report.PlayerID = Player.PlayerID
                WHERE                 
                <%= frmAttributesSearch.buildFilterString() %>                    
                ORDER BY 
                    ReportDate
       </String>
        Try '@txtNumber>>> was at the end of buildFilterString
            Using conn As New SqlClient.SqlConnection(DBConnection)
                conn.Open()
                Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                    Using daResults As New SqlClient.SqlDataAdapter(cmdQuery)
                        daResults.Fill(dt)
                    End Using
                End Using
            End Using

        Catch ex As Exception
            MsgBox("Filter Search Exception: " & ex.Message & vbNewLine & Str)
        End Try

        Return dt

    End Function
Nick
  • 37
  • 1
  • 1
  • 7
  • Why not pass the data to the module function. You are most likely dealing with a instance of this form rather than the default instance. – OneFineDay Feb 18 '15 at 20:56
  • Which data do you mean? I thought the data was being retrieved in the module through the search() function. – Nick Feb 18 '15 at 21:04
  • Poss the `frmAttributesSearch.buildFilterString()` part. – OneFineDay Feb 18 '15 at 21:06
  • Just cause it is legal to call `frmAttributesSearch.buildFilterString()` does not mean the module is connecting to this instance of the form that is running and has all the controls built. – OneFineDay Feb 18 '15 at 21:08
  • Add `frmAttributesSearch.Show()` to your search() method to see the instance back that you lost track of. – Hans Passant Feb 18 '15 at 21:17
  • As you said, I just did this dgPlayers.DataSource = search(buildFilterString) – Nick Feb 18 '15 at 21:22

1 Answers1

0

Try sending the filters to the function.

Public Shared Function search(filters As String) As DataTable
Dim dt As New DataTable
    Dim Str As String = _
   <String> SELECT 
                *
            FROM 
                Player
            INNER JOIN Report ON Report.PlayerID = Player.PlayerID
            WHERE                 
            <%= filters %>                    
            ORDER BY 
                ReportDate
   </String>
    Try '@txtNumber>>> was at the end of buildFilterString
        Using conn As New SqlClient.SqlConnection(DBConnection)
            conn.Open()
            Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                Using daResults As New SqlClient.SqlDataAdapter(cmdQuery)
                    daResults.Fill(dt)
                End Using
            End Using
        End Using

    Catch ex As Exception
        MsgBox("Filter Search Exception: " & ex.Message & vbNewLine & Str)
    End Try

    Return dt

End Function

Usage:

dgPlayers.DataSource = search(buildFilterString())
OneFineDay
  • 9,004
  • 3
  • 26
  • 37