Click here to Skip to main content
15,879,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay so I can't seem to figure out what is wrong with the query I'm trying to do.

Here is the code I have


string name;
name = cbxnames.SelectedValue.ToString();
myConnection.GetConnection();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE name= "'+name+'", myConnection.GetConnection());



What i need the query to look like to actually return somthing from my db is
"SELECT * FROM users WHERE name = 'randomname'"


But I cannot figure out how to escape the strings to make it do that.
Posted

1 solution

you have the quotes and double quote reversed

should be:
C#
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE name='"+name+"'", myConnection.GetConnection());
 
Share this answer
 
v2
Comments
Tunacha 29-Oct-10 13:17pm    
i have tried that way aswell and it doesnt seem to pull anything but when i put an actual name in the query it works fine
cmanderson 29-Oct-10 13:23pm    
Make sure to double check the value name contains. Any whitespace may mess it up. Debug into this and make sure the query string is what you expected.
Tunacha 29-Oct-10 13:33pm    
do u think it could possibly be the name variable?
William Winner 29-Oct-10 13:59pm    
either throw a messagebox in and output the name value, or step through your code in the debugger and see what value you are actually getting for name. That could be your problem.

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