[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:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(MyDataset11)
cbPublisher.DataSource = MyDataset11.Tables(0)
cbPublisher.DisplayMember = "Publisher"
cbPublisher.ValueMember = "Publisher"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim pub As String
pub = cbPublisher.Text
Dim dtBooks As New DataTable
Dim foundrows() As DataRow
dtBooks = MyDataset11.Tables(0)
Dim sqlString As String = "SELECT * FROM Book WHERE pub = " & "'" & pub & "'" & ""
foundrows = dtBooks.Select("Publisher = 'sqlString'")
End Sub
Re: Filter Datatable using Combo Box
Try
VB Code:
Dim sqlString As String
sqlString = "SELECT * FROM Book WHERE pub = '" & pub & "' "
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
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:
foundrows = dtBooks.Select(sqlString)
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:
Dim dtBooks As New DataTable
Dim foundrows() As DataRow
Dim sqlString, pub As String
pub = cbPublisher.Text
sqlString = "SELECT * FROM Book WHERE pub = '" & pub & "' "
dtBooks = MyDataset11.Tables(0)
foundrows = dtBooks.Select(sqlString)
Still there is no joy. Can some please HELP!!