Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 2-Jan-13 7:42am
Edited 2-Jan-13 7:43am
v2
Comments
ridoy at 2-Jan-13 14:28pm
   
What is the error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try changing that line to
 
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 %)
  Permalink  
Comments
ridoy at 2-Jan-13 14:29pm
   
exactly..+5
Sheikh Muhammad Haris at 2-Jan-13 18:29pm
   
My 5 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Comments
__TR__ at 2-Jan-13 14:26pm
   
My 5!
Mika Wendelius at 4-Jan-13 15:05pm
   
Thanks :)
ridoy at 2-Jan-13 14:29pm
   
Great..+5
Mika Wendelius at 4-Jan-13 15:05pm
   
Thank you :)
Sheikh Muhammad Haris at 2-Jan-13 18:29pm
   
My 5
Mika Wendelius at 4-Jan-13 15:06pm
   
Thanks :)

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

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 RyanDev 270
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 185
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100