Results 1 to 5 of 5

Thread: [RESOLVED] Filter Datatable using Combo Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    13

    Resolved [RESOLVED] Filter Datatable using Combo Box

    Hello There. I am a bit stuck here I need URGENT HELP!!
    I have a combo box that I populated with "Publisher" column in form lord. I want to use the combo box to fliter the Datagrid. Here is my code. When I click Button to show the filtered results, nothing happens.
    VB Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
    2. System.EventArgs) Handles MyBase.Load
    3.  
    4.         OleDbDataAdapter1.Fill(MyDataset11)
    5.         cbPublisher.DataSource = MyDataset11.Tables(0)
    6.         cbPublisher.DisplayMember = "Publisher"
    7.         cbPublisher.ValueMember = "Publisher"
    8.  
    9. End Sub
    10.  
    11. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    12. System.EventArgs) Handles Button1.Click
    13.         Dim pub As String
    14.         pub = cbPublisher.Text
    15.         Dim dtBooks As New DataTable
    16.         Dim foundrows() As DataRow
    17.         dtBooks = MyDataset11.Tables(0)
    18.         Dim sqlString As String = "SELECT * FROM Book  WHERE pub = " & "'" & pub & "'" & ""
    19.         foundrows = dtBooks.Select("Publisher = 'sqlString'")
    20. End Sub
    Last edited by Hack; Jan 15th, 2007 at 08:03 AM. Reason: Added vbcode tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Filter Datatable using Combo Box

    Try
    VB Code:
    1. Dim sqlString As String
    2.  
    3. sqlString = "SELECT * FROM Book  WHERE pub = '" &  pub & "' "

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    13

    Re: Filter Datatable using Combo Box

    There is something wrong with my code somewhere. I have tried
    It's still not working. It's not showing any errors but no filtering is being done. It now looks like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sqlString, pub As String
    sqlString = "SELECT * FROM Book WHERE pub = '" & pub & "' "
    pub = cbPublisher.Text

    Dim dtBooks As New DataTable
    Dim foundrows() As DataRow
    dtBooks = MyDataset11.Tables(0)
    foundrows = dtBooks.Select("Publisher = 'sqlString'")
    End Sub

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Filter Datatable using Combo Box

    First off, to make the code look good on the forums, use VBCode tags (which Hack added to your first post in this thread). To do this either use the button in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [vbcode] 'code here [/vbcode]


    The first issue in your code is that you are setting the "pub" variable after you are using it - it needs to be the other way around.

    Next up is how you run the SQL statement... I don't use VB.Net so I dont know the correct method, but what you have looks very wrong to me (as you are basically implying that you want records where the Publisher field has a value of "sqlString" [not anything to do with the variable called sqlString]). I presume that what you need is something more like this:
    VB Code:
    1. foundrows = dtBooks.Select(sqlString)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    13

    Re: Filter Datatable using Combo Box

    Hi Si_the_Geek

    Thanks for reply. I have made the changes you mentioned my code is now giving this error
    "Additional information: Syntax error: Missing operand after 'Book' operator."This my code:
    VB Code:
    1. Dim dtBooks As New DataTable
    2.         Dim foundrows() As DataRow
    3.         Dim sqlString, pub As String
    4.  
    5.         pub = cbPublisher.Text
    6.         sqlString = "SELECT * FROM Book  WHERE pub = '" & pub & "' "
    7.         dtBooks = MyDataset11.Tables(0)
    8.         foundrows = dtBooks.Select(sqlString)

    Still there is no joy. Can some please HELP!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width