Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my query and i m not able to escape special characters from the query .. i like to search from a textbox in gridview i need result as like"%somevalue%" my query is not coming right plz help ..


Select ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId where ProductName like'[%]"+SearchTextBox.Text+"[%]'
Posted

try this

string query=string.Format("Select ProductName
,Description,Image
,CategoryName
,IsActive
from Product
inner join Category on Product.CategoryId=Category.CategoryId
where Product
Name like '%{0}%'",SearchTextBox.Text.Replace("'","''"));
 
Share this answer
 
v2
Comments
Member 10949397 16-Jul-14 3:41am    
thanku sir ...i got it ..its working its escaping the special characters but my query like'%somevalue%' is not working properly ...thankzz once again
This will work,

declare @searchtext varchar(10)='in'
--declare @searchtext varchar(10)='%'
select * from holidaytable where name like CASE When @searchtext='%' Then '' Else '%'+@searchtext+'%' End
 
Share this answer
 
you should have to write string always in string.Format

SQL
 string query=string.Format("Select ProductName
,Description,Image
,CategoryName
,IsActive
from Product
inner join Category on Product.CategoryId=Category.CategoryId
where Product
Name like '%{0}%'",SearchTextBox.Text.Trim());
 
Share this answer
 
v2
Comments
Member 10949397 16-Jul-14 2:47am    
Thankz 4 for reply and suggestion but still it is not working
jo.him1988 16-Jul-14 3:07am    
ok you have to replace your ' to '' because its throw exception
SQL
"Select ProductName
,Description,Image
,CategoryName
,IsActive
 
from Product
inner join Category on Product.CategoryId=Category.CategoryId
where ProductName like'%"+SearchTextBox.Text.Replace("%", "[%]")+"%'"
 
Share this answer
 
v2
Comments
Member 10949397 16-Jul-14 2:05am    
Thanks sir for ur reply i tried this. and my query works perfectly fine . but when i enter % and ' in my search textbox no results should be displayed but whole gridview is getting displayed. i dont want any results when i type % and ' in textbox as there are no special characters added to my gridview nor database .What to do .?
Prasad Avunoori 16-Jul-14 3:28am    
View my updated solution. It should work.
Member 10949397 16-Jul-14 3:49am    
thankuu sir its working ....! but when i enter ' its not ... can u please help ..thankz ..!
Prasad Avunoori 16-Jul-14 4:48am    
You are supposed to use one more Replace() method.

SearchTextBox.Text.Replace("%", "[%]").Replace("'","''")
Member 10949397 16-Jul-14 4:54am    
Thankyou so mucchh ..!!!! my problem is solved now ..:)

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