Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written query:

SqlCommand cmd = new SqlCommand("select * from profile where name LIKE" +"%"+TextBox1.Text+ "%" , cnn);


this will give me error.what is problem in this?
Posted
Updated 2-Jan-13 7:43am
v2
Comments
ridoy 2-Jan-13 14:28pm    
What is the error?

Try changing that line to

SQL
SqlCommand cmd = new SqlCommand("select * from profile where name LIKE " +"'%"+TextBox1.Text+ "%'" , cnn);


You are missing a space (next to LIKE) and single quotes (before and after %)
 
Share this answer
 
Comments
ridoy 2-Jan-13 14:29pm    
exactly..+5
[no name] 2-Jan-13 18:29pm    
My 5 :)
As stated in the previous solution, the problem is the missing single quotes.

However, the correction is to properly use parameters (see SqlParameter[^]).

So the code should be something like:
C#
SqlCommand cmd = new SqlCommand("select * from profile where name LIKE @name" , cnn);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = string.Format("%{0}%", TextBox1.Text);
...

Without using parameters, you will be open to SQL injections, illegal character problems (single quote for example) etc.
 
Share this answer
 
Comments
__TR__ 2-Jan-13 14:26pm    
My 5!
Wendelius 4-Jan-13 15:05pm    
Thanks :)
ridoy 2-Jan-13 14:29pm    
Great..+5
Wendelius 4-Jan-13 15:05pm    
Thank you :)
[no name] 2-Jan-13 18:29pm    
My 5

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