| | |
Syntax error in "like" in sql statement
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
Hi,
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
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)
I've also tried
That didn't work either. Can someone please provide me a correct syntax to run the above? Thanks.
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
ASP.NET Syntax (Toggle Plain Text)
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)
I've also tried
ASP.NET Syntax (Toggle Plain Text)
mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%'"
That didn't work either. Can someone please provide me a correct syntax to run the above? Thanks.
Last edited by tgreer; Oct 7th, 2006 at 10:37 am.
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
Hi ManicCW,
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.
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.
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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?
ASP.NET Syntax (Toggle Plain Text)
mstrSql = mstrSql & " where staffID like '%" & txtStaffID.Text.Trim() & "%' "
ASP.NET Syntax (Toggle Plain Text)
mstrSql = "Select * from Staff"
ASP.NET Syntax (Toggle Plain Text)
mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
•
•
•
•
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
whereASP.NET Syntax (Toggle Plain Text)
mstrSql = mstrSql & " where staffID like '%" & txtStaffID.Text.Trim() & "%' "
And, because I forgot to place a space before the "where" word, so it'd become "Select * from StaffWhere". Some careless mistake. Anyway, thisASP.NET Syntax (Toggle Plain Text)
mstrSql = "Select * from Staff"
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.ASP.NET Syntax (Toggle Plain Text)
mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
But that depends on the application and its design.
![]() |
Similar Threads
- SQL statement error (ASP.NET)
- syntax error: i cant find it..HELP! (ASP)
- declaration syntax error? (C++)
- syntax error that I just can't seem to find! (Visual Basic 4 / 5 / 6)
- UPDATE syntax error (MySQL)
Other Threads in the ASP.NET Forum
- Previous Thread: uploading Images into Database
- Next Thread: Weird thing with ACCESS....weird
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news novell numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers





