简体   繁体   中英

SELECT Query WHERE multiple values from checkboxlist are used

I was wondering if it was possible to filter down data from a table using multiple values from a checkboxlist ? (or any other way) I have a checkboxlist and a gridview and when you check on of the boxes it does show the right data in the gridview but the problem arises when I try to check multiple values. It seems to search for the first checked value and then ignores the rest. You'd think it'd be simple! Perhaps it is. Here is my attempt below.

CODE BEHIND

Imports System.Data

Imports System.Data.SqlClient

Partial Class Default2

Inherits System.Web.UI.Page

Dim strSQL As New System.Text.StringBuilder


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            Dim i As Integer, c As Integer = 0
            Dim strParams As String = ""
            For i = 0 To Me.CheckBoxList1.Items.Count - 1
                If CheckBoxList1.Items(i).Selected Then
                    c += 1
                    If c = 1 Then
                        strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    Else
                        strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    End If
                End If
            Next
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
            FillGridView()



        End If
    End Sub


    Private Sub FillGridView()

        Dim strMyConn As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True"
        Using MyConn As New SqlClient.SqlConnection(strMyConn)
            MyConn.Open()
            Dim cmd As New SqlClient.SqlCommand(strSQL.ToString, MyConn)
            cmd.Connection = MyConn
            cmd.CommandType = CommandType.Text
            Try
                Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
                    Dim dt As New DataTable
                    dt.Load(dr)


                    Me.GridView1.DataSource = dt
                    Me.GridView1.DataBind()
                End Using
                If Me.GridView1.Visible = False Then Me.GridView1.Visible = True


            Catch ex As Exception
                Me.GridView1.Visible = False
            End Try
        End Using
    End Sub


    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer, c As Integer = 0
        Dim strParams As String = ""
        For i = 0 To Me.CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
                c += 1
                If c = 1 Then
                    strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                Else
                    strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                End If
            End If
        Next
        If c <> 0 Then
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
        End If




    End Sub
End Class

Refactor this section to create a WHERE IN statement so it checks to see if the value is found among any item checked

Before

Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        c += 1
        If c = 1 Then
            strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        Else
            strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        End If
    End If
Next

After

Dim params As StringBuilder = New StringBuilder()

For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        params.Append("'") 
        params.Append(CheckBoxList1.Items(i).Text)
        If i < Me.CheckBoxList1.Items.Count Then
            params.Append("',") // don't append a comma if it's the last item
        End If
    End If
Next
strSQL.Append("SELECT Project.* FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID WHERE Keyword.Keyword in (")
strSQL.Append(params.ToString()) // append comma delimited values that make up where in statement
strSQL.Append("')") // close where in statement
FillGridView()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM