N
Nathan Sokalski
I am trying to do a database search using LIKE using the following code:
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
If Page.IsValid Then
Dim logictype As String
Dim keywords As String()
Dim papdatabase As New DataSet
Dim myconnection As System.Data.OleDb.OleDbConnection = New
OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &
Server.MapPath("papresenters.mdb"))
Dim cmdSelect As New System.Data.OleDb.OleDbCommand
Dim dataadapterSelect As New System.Data.OleDb.OleDbDataAdapter
cmdSelect.Connection = myconnection
dataadapterSelect.SelectCommand = cmdSelect
If radAll.Checked Then logictype = " AND " Else logictype = " OR "
cmdSelect.CommandText = "SELECT
members.organization,members.artist,artists.email,artists.website FROM
members,artists WHERE "
If Not chkAnyDate.Checked Then
cmdSelect.CommandText &= "#" & calTimeFrame.SelectedDate.ToShortDateString()
& "#>timeframestart AND #" & calTimeFrame.SelectedDate.ToShortDateString() &
"#<timeframeend OR "
cmdSelect.CommandText &= "#" &
calTimeFrame2.SelectedDate.ToShortDateString() & "#>timeframestart AND #" &
calTimeFrame2.SelectedDate.ToShortDateString() & "#<timeframeend"
End If
If txtArtist.Text <> "" Then
cmdSelect.CommandText &= logictype & "artist='" & txtArtist.Text & "'"
End If
keywords = txtKeyword.Text.Split(",".ToCharArray)
For Each word As String In keywords
word = word.Trim()
cmdSelect.CommandText &= logictype & "organization LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "county LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "artist LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "genre LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "comments LIKE '%" & word & "%'"
Next
dataadapterSelect.Fill(papdatabase)
datResults.DataSource = papdatabase
datResults.DataBind()
Me.Controls.Add(New LiteralControl("The page is valid!"))
Else
Me.Controls.Add(New LiteralControl("The page is not valid!"))
End If
End Sub
This gives me a syntax error in the query expression containing the LIKE's.
What is it that I am doing wrong here? Thanks.
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
If Page.IsValid Then
Dim logictype As String
Dim keywords As String()
Dim papdatabase As New DataSet
Dim myconnection As System.Data.OleDb.OleDbConnection = New
OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &
Server.MapPath("papresenters.mdb"))
Dim cmdSelect As New System.Data.OleDb.OleDbCommand
Dim dataadapterSelect As New System.Data.OleDb.OleDbDataAdapter
cmdSelect.Connection = myconnection
dataadapterSelect.SelectCommand = cmdSelect
If radAll.Checked Then logictype = " AND " Else logictype = " OR "
cmdSelect.CommandText = "SELECT
members.organization,members.artist,artists.email,artists.website FROM
members,artists WHERE "
If Not chkAnyDate.Checked Then
cmdSelect.CommandText &= "#" & calTimeFrame.SelectedDate.ToShortDateString()
& "#>timeframestart AND #" & calTimeFrame.SelectedDate.ToShortDateString() &
"#<timeframeend OR "
cmdSelect.CommandText &= "#" &
calTimeFrame2.SelectedDate.ToShortDateString() & "#>timeframestart AND #" &
calTimeFrame2.SelectedDate.ToShortDateString() & "#<timeframeend"
End If
If txtArtist.Text <> "" Then
cmdSelect.CommandText &= logictype & "artist='" & txtArtist.Text & "'"
End If
keywords = txtKeyword.Text.Split(",".ToCharArray)
For Each word As String In keywords
word = word.Trim()
cmdSelect.CommandText &= logictype & "organization LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "county LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "artist LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "genre LIKE '%" & word & "%'"
cmdSelect.CommandText &= logictype & "comments LIKE '%" & word & "%'"
Next
dataadapterSelect.Fill(papdatabase)
datResults.DataSource = papdatabase
datResults.DataBind()
Me.Controls.Add(New LiteralControl("The page is valid!"))
Else
Me.Controls.Add(New LiteralControl("The page is not valid!"))
End If
End Sub
This gives me a syntax error in the query expression containing the LIKE's.
What is it that I am doing wrong here? Thanks.