简体   繁体   中英

applying multiple filters in listview

I have listview and onPage load all data gets displayed in it. To filter listview I have 4 types of RedioButtonList (area, service, facility, payment). Now suppose I apply first filter ie area then it works fine but after that I apply service Filter than area goes blank. It should find selected service on selected area. After debugging I seen what query actually passes is as below

select * from table where areaName = '' and services = 'selected service'

but it should be go like this

select * from table where areaName = 'SelectedArea' and services = 'selected service'

Following function I am running On each filter IndexChanged

Private Sub getResult()
    Try
        Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
        Dim where As String = String.Empty
        Session("Data") = Nothing
        If areasList.SelectedValue <> "All" Then
            where = "address like '%" & areasList.SelectedValue & "%'"
        End If

        If servicesList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "serviceID like '%" & servicesList.SelectedValue.ToString & "%'"
            Else
                where &= "and serviceID like '" & servicesList.SelectedValue.ToString & "%'"
            End If
        End If

        If facilitiesList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "facilities like '%" & facilitiesList.SelectedValue & "%'"
            Else
                where &= "and facilities like '" & facilitiesList.SelectedValue & "%'"
            End If
        End If

        If paymentsList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "payment like '%" & paymentsList.SelectedValue.ToString & "%'"
            Else
                where &= "and payment like '%" & paymentsList.SelectedValue.ToString & "%'"
            End If
        End If

        Dim query As String = "SELECT hospitalID, name, address, thumbnail, knownFor, mondayFrom, mondayTo, consultancyFees FROM hospitals where city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
        If Not String.IsNullOrEmpty(where) Then
            query &= " and " & where
        End If

        Dim cmd As New MySqlCommand(query, con)
        Dim da As New MySqlDataAdapter(cmd)
        Dim table As New DataTable
        da.Fill(table)
        Session("Data") = table
        hospitals.DataSource = table
        hospitals.DataBind()
    Catch ex As Exception
        Response.Write(ex)
    End Try
End Sub

Where I am doing wrong please help.

Your where clause is being reset each time you change the index on one of your dropdown boxes. You need to create a function which will be common to all your dropdown boxes that will loop through the values of each drop down box and create a where with all your variables.

dim where as string;        
Private Sub changeWherestatement(sender As Object, e As EventArgs) Handles    ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
where=string.empty
 If areasList.SelectedValue <> "All" Then
        where = "address like '%" & areasList.SelectedValue & "%'"
    End If
If servicesList.SelectedValue <> "All" Then
        If String.IsNullOrEmpty(where) Then
            where += " and serviceID like '%" & servicesList.SelectedValue.ToString & "%'"
        Else
            where += "and serviceID like '" & servicesList.SelectedValue.ToString & "%'"
        End If
    End If
Etc

End Sub

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