We help IT Professionals succeed at work.

How Can I Write An SQL  "Select" Query To Select Based On The Text Entered By The User In The "Textbox" Control?

172 Views
Last Modified: 2013-11-26
Greetings Everyone!
I am really stuck at the moment and can't figure out this. I hope someone could help. this is what I am trying to do:
I have a search "Form", it is a Windows Application Form, and what I want to do is to let the user enter in a "TextBox" control the first name of a customer, and then during runtime excute an sql query to, it is a "SQL SELECT Query", I need this query to return the all the customer records from the Microsoft Access Datasbe Table, those reocrds where the customers full names start with the name the user has entered in the "TextBox" control on the form.

here is the code I wrote, as for testing the query, but I don't get any results, when I check the "SqlDataReader" using the "HasRows" property, it returns "False".
I have attached it in the code snippet.
now the strange thing, I have used the keyword "LIKE" in my query, and if I specify the full name of the customer which happens to be "Mary S. Jones" then I get "True", which means the query has returned some rows, but when I use "Mary*"  then I get "False". the first part of the code in the Code Snippet returns "False" but the second version the one bellow it, returns "True".

I am totally stuck, and I tackle this problem.
and thanks in advance guys!

~Salman~



'This code Returns "False"
 
 Dim myconn As New OleDbConnection(My.Settings.MasterFileConnectionString2)
        Dim mycommand As New OleDbCommand("select * from customers where name LIKE 'Mary*'", myconn)
        myconn.Open()
        mycommand.CommandType = CommandType.Text
        Dim myreader As OleDbDataReader
        myreader = mycommand.ExecuteReader
        myreader.Read()
        MsgBox(myreader.HasRows)
 
 
 
'This code returns "True"
 Dim myconn As New OleDbConnection(My.Settings.MasterFileConnectionString2)
        Dim mycommand As New OleDbCommand("select * from customers where name LIKE 'Mary S. Jones'", myconn)
        myconn.Open()
        mycommand.CommandType = CommandType.Text
        Dim myreader As OleDbDataReader
        myreader = mycommand.ExecuteReader
        myreader.Read()
        MsgBox(myreader.HasRows)

Open in new window

Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ohh! Thanks Surajgupatha! I took your advise and it worked with the precentage sign, truely unbelieveable!
thanks again!

~Salman~
You are welcome :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.