|
-
Jul 28th, 2011, 11:20 AM
#1
Thread Starter
Hyperactive Member
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
-
Jul 28th, 2011, 11:25 AM
#2
Re: SQL Select doesnt obey condition
this would work:
vb Code:
Dim SQL As String = "SELECT * FROM TblContacts WHERE Phone = '" & Value & "'"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2011, 03:03 PM
#3
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?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 28th, 2011, 06:09 PM
#4
Thread Starter
Hyperactive Member
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
-
Jul 28th, 2011, 06:15 PM
#5
Re: SQL Select doesnt obey condition
that's why stanav asked you:
What's the value of the resulting query string?
because it's obviously wrong
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2011, 07:36 PM
#6
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
-
Jul 29th, 2011, 09:14 AM
#7
Thread Starter
Hyperactive Member
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
-
Jul 29th, 2011, 09:32 AM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|