Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Filter gridview column with multiple comboxbox.text. Need someone to check code.

Posted on 2011-03-10
9
Medium Priority
?
609 Views
Last Modified: 2012-05-11
Hello experts,

I have a gridview with columns id, firstname, secondname. I have write below vb.net code to filter FirstName column with two or more checkboxes. I have add two checkboxes to try filter it.
Column values are:
John
James
Paul

Checkbox1 value  is "John".
Checkbox2 value is "James"

 The code is working, but not as I want to. Gridview is filtered if only  one checkbox is checked. But, I want if both checkboxes are check that I get rows which contains "John", and "James" values.
But, I also think that I'm doing something wrong, not the right way!?

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" & CheckBoxList1.Text & "%' and FIRSTNAME like '%" & CheckBoxList2.Text & "%'"
        SqlDataSource1.DataBind()

Open in new window


Thank you in advance for help to anyone.
0
Comment
Question by:dejandejanovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35101521
the problem is that you don't check if checkbox is checked or not.
secundly, checkbox text property is constant and doesn't rely with checked state.

so, to end that explication, this is a possible query :

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE ( FIRSTNAME like '%" & CheckBoxList1.Text & "%'  AND " & CheckBoxList1.checked & ")  OR (FIRSTNAME like '%" & CheckBoxList2.Text & "%' AND " & CheckBoxList2.checked & ")"
        SqlDataSource1.DataBind()

example : ck1 checked and ck2 not checked :
the query is SELECT * FROM MAIN WHERE (FIRSTNAME = PAUL and 1) OR (FIRSTNAME = ALAN and 0)
that's equal to : SELECT * FROM MAIN WHERE (FIRSTNAME = PAUL and 1) OR 0
after boolean simplify :  SELECT * FROM MAIN WHERE FIRSTNAME = PAUL

hope my french translated will help you !
0
 

Author Comment

by:dejandejanovic
ID: 35111940
Hi, and thanks for reply.

I have try with your solution, but getting this error as printscreen show.

Please advise, thanks.
printscreen.jpg
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35113499
oups I read "checkbox" and that's checkboxlist ! sorry for that mistake :

you must iterate thru items in checkboxlist


Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
           
 Dim i As Integer

         For i=0 To checkboxlist1.Items.Count - 1

            If checkboxlist1.Items(i).Selected Then

               SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" &  checkboxlist1.Items(i).Text

            End If

         Next

end sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35113519
and don't forget to put onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" in aspx tags like this :

 <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" >
            <asp:ListItem>toto</asp:ListItem>
            <asp:ListItem>titi</asp:ListItem>
            <asp:ListItem>tata</asp:ListItem>
        </asp:CheckBoxList>

have a good WE !
0
 

Author Comment

by:dejandejanovic
ID: 35131840
Hm, the code is working, but not as it should. It filter gridview, but only by one item, it return rows only from one value. But, still I would like if  I have 5 items, that it would return if select 2 of them to return rows of both values.

By the way, thanks for helping.

   Dim i As Integer

        For i = 0 To CheckBoxList1.Items.Count - 1

            If CheckBoxList1.Items(i).Selected Then

                SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"

            End If

        Next

    End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35175647
I was in hollidays sorry : I will reply tomorrow ! thanks !
0
 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 2000 total points
ID: 35183476
OK, you can add 'OR' statements to query like this :

dim MyFilter as string = ""

Dim i As Integer

        For i = 0 To CheckBoxList1.Items.Count - 1

            If CheckBoxList1.Items(i).Selected Then

      If MyFilter = "" then
                 MyFilter = " WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
      Else
                 MyFilter = MyFilter & " OR FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
      End If
           
           End If

        Next

End Sub

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN " & MyFilter
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35183522
PS : obviously 'End sub' must be at end of proc. (misplaced in my sample).
0
 

Author Closing Comment

by:dejandejanovic
ID: 35184345
Heheheh, ti works. Now it works perfectly, also if I add another Item.

Tuyau2poil, thanks million times. I have also learned a lot from this post.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question