Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good evening
During the execution of my application in vb.net (SQL database server) is a syntax error appears ..
code: Dim cd As New SqlClient.SqlCommand("select * from Fournisseur where Code=" + ComboBox2.Text, cnx)

Error (Invalid syntax near '=')



Please, Can you help me solve this problem?
Posted

Use SqlCommand rather than string concatenation. Not sure that is your problem, but it is a problem anyway.
 
Share this answer
 
Either your Code field is a string and the literal value coming from the ComboBox needs double quotes, or the Code field isn't a string and the value coming from the ComboBox needs parsing (and possibly a pair of delimiters, that depends on type). Either way it is wrong.

And, as aspdotnetdev already said, using a parameterized SQL command is recommended (it would still require parsing when the value from the ComboBox isn't intended as a string).

:)
 
Share this answer
 
Hi,

SQL
Dim cd As New SqlClient.SqlCommand("select * from Fournisseur where Code=" + ComboBox2.Text, cnx)


replace with '&' instead of '+'

SQL
Dim cd As New SqlClient.SqlCommand("select * from Fournisseur where Code=" & ComboBox2.Text, cnx)
 
Share this answer
 
Hi try this

Dim cd As New SqlClient.SqlCommand("select * from Fournisseur where Code='" + ComboBox2.Text+"'", cnx)


the string literal should be in Quotes.

Regards,
Jitendra
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900