Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I am trying to search name using text box and it is not searching

What I have tried:

C#
protected void txt_SearchName_TextChanged1(object sender, EventArgs e)
       {
           cn.Open();
           SqlCommand cmd = new SqlCommand("select * from gvdetails17 where EmpName LIKE % '" + txt_SearchName.Text + "'%", cn);
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           gvDetails.DataSource = dt;
           cn.Close();
       }

ASP.NET
<pre> <asp:TextBox ID ="txt_SearchName" runat="server" style="margin-bottom: -71px;
 margin-left:308px" Width="120px" Font-Size="12px" OnTextChanged= "txt_SearchName_TextChanged1" AutoPostBack="false"></asp:TextBox>
Posted
Updated 27-Feb-17 2:38am

try
protected void txt_SearchName_TextChanged1(object sender, EventArgs e)
     {
             string name = txt_SearchName.Text.Trim();
             if(name != "") {
             SqlCommand cmd = new SqlCommand("select * from gvdetails17 where EmpName Like '%'+ @name + '%' ", cn);
             cmd.Parameters.Add("@name", name);
             DataTable dt = new DataTable();
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             da.Fill(dt);
             gvDetails.DataSource = dt;
             gvDetails.DataBind();

         }
     }
 
Share this answer
 
v2
Comments
Member 12605293 27-Feb-17 8:44am    
Can I wrap that into my method BindData()
Karthik_Mahalingam 27-Feb-17 8:50am    
yes with some correction..
Member 12605293 27-Feb-17 8:58am    
private void BindGrid()
{
DataTable dt = new DataTable();

string query = " select * from gvdetails17 WHERE 1=1 And EmpName Like '%'+ @name + '%' ";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;


if (ddlAddSalary1.SelectedValue != "" && ddlAddSalary1.SelectedValue != "--Select--")
{

string[] sal = ddlAddSalary1.SelectedValue.Split('-');
string from = sal[0];
string to = sal[1];


query += " and Sal between @fromsal and @tosal ";
cmd.Parameters.AddWithValue("@fromsal", from);
cmd.Parameters.AddWithValue("@tosal", to);

}
if (ddlAge.SelectedValue != "" && ddlAge.SelectedValue != "--Select--")
{

string[] age = ddlAge.SelectedValue.Split('-');
string from1 = age[0];
string to1 = age[1];

query += " and Age between @fromage and @toage ";
cmd.Parameters.AddWithValue("@fromage", from1);
cmd.Parameters.AddWithValue("@toage", to1);

}


if (ddlDepartment.SelectedValue != "" && ddlDepartment.SelectedValue != "--Select--")
{
query += " and Dep = @Dep ";
cmd.Parameters.AddWithValue("@Dep", ddlDepartment.SelectedValue);
}

string name = txt_SearchName.Text.Trim();
if (name != "")
{
query += " and name = @name ";
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}}

protected void txt_SearchName_TextChanged1(object sender, EventArgs e)
{

BindGrid();
Member 12605293 27-Feb-17 8:59am    
I asks me to declar scalar variable and the texbox
Karthik_Mahalingam 27-Feb-17 9:02am    
Ok declare it
The percentage wildcard characters must be inside the quotes:
C#
SqlCommand("select * from gvdetails17 where EmpName LIKE '% " + txt_SearchName.Text + "%'", cn);
 
Share this answer
 
Never do that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Additionally, the quote is in the wrong place.
The chances are that using a parameterised query will remove your problem - and don't forget the rest of your code: one missed command and anyone can delete your database.
C#
using (SqlCommand cmd = new SqlCommand("SELECT * FROM gvdetails17 WHERE EmpName LIKE '%' + @Txt + '%'", cn))
   {
   cmd.Parameters.AddWithValue("@Txt", txt_SearchName.Text);
   ...
 
Share this answer
 
Comments
Ehsan Sajjad 27-Feb-17 6:07am    
do we still need contatenation when using parameterzied query?
OriginalGriff 27-Feb-17 6:14am    
That isn't using concatenation - it's telling SQL to use the parameters value instead of concatenating it.
When you do this:
string s = "SELECT * FROM MyTable WHERE column = '" + myTextBox.Text + "'";
You pass through the textbox value as part of the command, so if it contains value SQL, the the server will object it.
So If I type

x';DROP TABLE gvdetails17;--

in the textbox, SQL sees it as three commands:

SELECT * FROM MyTable WHERE column = 'x'
DROP TABLE gvdetails17
--'

Where the last is an SQL comment.
It does the select, deletes your table, and ignores the rest of the command.

Never, ever, do that!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900