Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All
I'm Creating Simple Search Page Using Textbox and Button
When I try Searching , The results Show only when I type Numbers in textbox

This is my Sql
C#
Select Name from Users Where Name Like '%{0}%'

Note : {0} is the Field Number in Method (Class)
I call it's Function in code behind and pass textbox.text

it is not the problem
The method Working Correctly
But the problem :
I have The following users in table :
Maria 56 - Barak - Rano30
But When I try To type in textbox Maria --> the result is nothing
When I try to type Maria 56 --> the result is nothing
But when I type in textbox 56 the result show in gridview

anyone can help me ???

What I have tried:

I tried changing in Select SQL But nothing changed
Posted
Updated 15-Aug-16 15:04pm
Comments
Karthik_Mahalingam 16-Aug-16 2:08am    
select * from table where name like '%maria%'
this is working..
shms_rony 16-Aug-16 7:04am    
Not working , only numbers or space in textbox show results of execution
may be there is something error in textbox , could you help me
shms_rony 16-Aug-16 7:11am    
but also when I write Maria in label text and " select where name like '%'+label1.text+'%' , the same problem :/
Karthik_Mahalingam 16-Aug-16 7:45am    
try running the query in sql server studio and check
Karthik_Mahalingam 16-Aug-16 7:44am    
sure, but you will to provide the info clearly.

1 solution

You haven't given us must code to look at so I can only conclude its your SQL statement with the {0} field number.

We cant see what this class is or what it does, but you should check the returned SQL statement to see if its a valid sql statement before running it against the database. The statement above is not unless you are searching for {0} in the name field.

Maybe what you are after is
SQL
Select Name from Users Where Name like '%[0-9]%' and name like '%' + textbox.text + '%'
 
Share this answer
 
v3
Comments
shms_rony 15-Aug-16 21:12pm    
class method
==================
public DataTable searchname(string value)
{
string query = String.Format("Select Name from Users Where Name Like '%{0}%'", value);

return search(query);
}


in code behind
=====================

protected void ButSrch_Click(object sender, EventArgs e)
{

GridView5.DataSource = classobject.searchname(Textsrch.Text);

GridView5.DataBind();


}
RossMW 15-Aug-16 21:23pm    
See modified answer above. Depending on what search conditions you are trying to achieve, you can have various combinations of above. But essentially you need to ensure the SQL is valid and run it manually across the database and correct until you get the desired result.
shms_rony 15-Aug-16 21:29pm    
how can I use your modified answer in my sql ?
RossMW 15-Aug-16 21:37pm    
Answer has changed with full sql statement. Your DAtaTable code will need to be changed.

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