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