I've got a problem with searching for record(s) that match the staffID in my "Staff" table in the database. I have a string named mstrSql, a txtStaffID text box and a sql statement that goes
mstrSql = "Select * from Staff where staffID like '" & txtStaffID.Text.Trim() & "%" & "'"
I ran the code and it returned an error: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'like'. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
Thanks, but I have solved the problem eventually. The type of my staffID column is varchar with length of 4. The second way which i have posted turned out to be correct, it couldn't run correctly in the first place was because of some mistake in the subsequent code. And, I have tried to replace the '%' sign with '*', thinking it might work. However, it didn't and the '%' sign is correct.
it couldn't run correctly in the first place was because of some mistake in the subsequent code.
What mistake? Is the query string what you thought it was? What happens if you print the query instead of run it and then paste it into SQL to run manually?
What mistake? Is the query string what you thought it was? What happens if you print the query instead of run it and then paste it into SQL to run manually?
Uh, actually it is hard for me to describe my mistake without showing you the entire thing. I actually need to concatenate the sql with another sql statement, like this one
mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
can run. I mean, there's no mistake in the syntax near the "like". Hope I did answer your question. Of course, if you paste the sql into sql query, it would have to be without the '&' and txtStaffID value.
Uh, actually it is hard for me to describe my mistake without showing you the entire thing. I actually need to concatenate the sql with another sql statement, like this one
mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
can run. I mean, there's no mistake in the syntax near the "like". Hope I did answer your question. Of course, if you paste the sql into sql query, it would have to be without the '&' and txtStaffID value.
This is correct. But as an aside, it is not good practice to use the LIKE clause for something like a staffID, which should be unique or a primary key.
But that depends on the application and its design.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.