SQL Select doesnt obey condition
The following lines are supposed to display to a text box the phones numbers that match the condition in the sql statement, however it shows me all the phone numbers:
Code:
Dim SQL As String = "SELECT *, Phone FROM TblContacts WHERE '" & Event_query & Condition & Abs_value & "'"
Dim myOleDbCommand As New OleDb.OleDbCommand(SQL, con)
con.Open()
Using myDataReader As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader()
If myDataReader.Read() Then
While myDataReader.Read()
' txtPhone.Text =
numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
notfound = 0
End While
I feel like this line is the problem but I am not sure if I am missing something:
Code:
numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
Help would be much appreciated
Re: SQL Select doesnt obey condition
this would work:
vb Code:
Dim SQL As String = "SELECT * FROM TblContacts WHERE Phone = '" & Value & "'"
Re: SQL Select doesnt obey condition
What's the values of Event_query, Condition, Abs_value variables?
What's the value of the resulting query string?
Re: SQL Select doesnt obey condition
those are values from combo boxes. I am trying to make an application that allows the user to build their own queries from drop boxes. it works except it shows me all the phone numbers after the first one has met the condition, rather than just the ones that meet the condition
Re: SQL Select doesnt obey condition
that's why stanav asked you:
Quote:
What's the value of the resulting query string?
because it's obviously wrong
Re: SQL Select doesnt obey condition
Oh, I can tell you exactly what's wrong... your "query" is being treated as literal text....
Code:
Dim SQL As String = "SELECT *, Phone FROM TblContacts WHERE '" & Event_query & Condition & Abs_value & "'"
The ' in SQL denotes a string....
-tg
Re: SQL Select doesnt obey condition
Yeah cheers, removing the '' around the statement almost got it working perfectly. It seems to miss out the first one always that meets the criteria, but then displays all the rest that meet the critereia?
Code:
Dim SQL As String = "SELECT Phone FROM TblContacts WHERE " & Event_query & Condition & Abs_value
Dim myOleDbCommand As New OleDb.OleDbCommand(SQL, con)
con.Open()
Using myDataReader As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader()
If myDataReader.Read() Then
While myDataReader.Read()
' txtPhone.Text =
numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
notfound = 0
End While
Any ideas why that may be? P.S I also remove the *, after SELECT but that didnt fix it
Re: SQL Select doesnt obey condition
Code:
If myDataReader.Read() Then
While myDataReader.Read()
You're calling .Read twice ... the first one loads up the first row... but before you do anything with it, you're loading up the second row with the second read...
Try this:
Code:
If myDataReader.HasRows() Then
While myDataReader.Read()
.HasRows is a function that returns True/False indicating... yup, if the reader has any rows to read...
-tg