0

I am trying to populate a Combobox from a MySQL database; but I don't get anything. Below is the code.

Table: Class

Columns: Code, State

sqlstr = "SELECT * FROM Class WHERE State= Not Started"
DBCmd = New MySql.Data.MySqlClient.MySqlCommand(sqlstr, DBConn)
DBDR = DBCmd.ExecuteReader
While (DBDR.Read())
CB_Class.Items.Add(DBDR("Code"))
End While
DBCmd.Dispose()
DBDR.Close()

I believe the result is wrong, because there are at least 2 records with their state values set as "Not Started". What is wrong? Is there anything wrong with the way I've written "State= Not Started" ?

1
  • The program works fine until it reaches DBDR = DBCmd.ExecuteReader. It doesn't execute any further. Commented Apr 4, 2013 at 23:20

1 Answer 1

1

The command text doesn't seems correct. It lacks the single quote around the string to search

sqlstr = "SELECT * FROM Class WHERE State='Not Started'"
                                          ^           ^

If State is a string field then every search over it should be enclosed in single quotes.
Beware of the potential problems when the string to search contains a single quote.

In this simple case you could use directly the string constant, but if you render your search dynamic with user input then you should use parametrized queries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.