Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 text boxes on an asp page and one search button. Users can search by entering text in any of the search boxes. I am using gridview with item template and bind with dataset. please tell me how to search data by textbox. for ex: user can search by entering data in all 2 text boxes or 3 text boxes. please provide me any code or suggestion for asp.net c#

What I have tried:

C#
protected void SearchButton_Click(object sender, EventArgs e)
{
   string str = "Select [ITEM No#], [Company Name], [Discipline Required], [Service Description], Institution, [Award Date] from PSP_Report where ([ITEM NO#] like '%' + @search + '%' [Company Name] like '%' + @search1 + '%')";
   SqlCommand xp = new SqlCommand(str, con);

   xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = RefNo.Text;
   xp.Parameters.Add("@search1", SqlDbType.NVarChar).Value = RefNo.Text;

   con.Open();
   xp.ExecuteNonQuery();
   SqlDataAdapter da = new SqlDataAdapter();

   da.SelectCommand = xp;
   DataSet ds = new DataSet();
   da.Fill(ds, "ITEM No#");
   Search.DataSource = ds;
   Search.DataBind();
   con.Close();        
}
Posted
Updated 3-Mar-17 3:37am
v2
Comments
[no name] 3-Mar-17 8:50am    
What happens when you run this code? Do you get any errors? I do not believe that this is valid SQL.
Nomfundo Cindi 6-Mar-17 8:39am    
It doesn't display any errors, the problem is i get false results
Nomfundo Cindi 6-Mar-17 8:42am    
It doesn't display any errors, but it gives wrong results
Bryian Tan 3-Mar-17 8:53am    
the query is missing operator

@search + '%' [Company Name] should be @search + '%' OR [Company Name]
Nomfundo Cindi 6-Mar-17 8:40am    
I have triad that still doesn't work

1 solution

If I was doing this then I'd have some code like this:
C#
protected void Search_Click(object sender, EventArgs e)
  {

      var constr = ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString;

      var sql = new StringBuilder();
      sql.Append("Select [ITEM No#], [Company Name], [Discipline Required], [Service Description], Institution, [Award Date]");
      sql.Append(" from PSP_Report ");

      using (var con = new SqlConnection(constr))
      {
          using (var xp = new SqlCommand())
          {
              xp.Connection = con;

              var andRequired = false;
              var whereAdded = false;

              AddASearchItem(ref whereAdded, ref andRequired, sql, Search1.Text, "[ITEM NO#]", 1, xp);
              AddASearchItem(ref whereAdded, ref andRequired, sql, Search2.Text, "[Company Name]", 2, xp);
              AddASearchItem(ref whereAdded, ref andRequired, sql, Search3.Text, "[Discipline Required]", 3, xp);

              xp.CommandText = sql.ToString();

              con.Open();

              var da = new SqlDataAdapter {SelectCommand = xp};

              var ds = new DataSet();
              da.Fill(ds);
              GridView1.DataSource = ds.Tables[0];
              GridView1.DataBind();
              con.Close();
          }
      }
  }

  protected void AddASearchItem(ref bool whereAdded, ref bool andRequired, StringBuilder s,
      string searchText, string colName, int item, SqlCommand cmd)
  {
      //Add a search item to the string builder determining whether or not the words 'WHERE' or 'AND'
      //need to be added. Add an appropriate parameter to the sql command.
      if (string.IsNullOrEmpty(searchText)) return;
      if (!whereAdded)
      {
          s.Append(" where ");
          whereAdded = true;
      }
      else
          if (andRequired) s.Append(" AND");
      s.Append(string.Format("  {0} LIKE @search{1}", colName,item));
      cmd.Parameters.AddWithValue("@search" + item.ToString(), "%" + searchText + "%");
      andRequired = true;
  }

Points to note:
1 - I've used a StringBuilder to dynamically build up the sql command text. This is better than concatenating strings (because strings are immutable)
2 - I have a little function that only adds search items onto the SQL if the relevent search textbox has something in it.
3 - I need to work out if the words " WHERE " or " AND " need to be added to the SQL.
4 - If none of the search boxes have anything in them then all records will be returned
 
Share this answer
 
Comments
Nomfundo Cindi 6-Mar-17 9:03am    
It only finds the first textbox
CHill60 6-Mar-17 16:22pm    
I don't understand your comment
Nomfundo Cindi 7-Mar-17 1:31am    
@Chill60
The code you posted it runs without errors, but it only searches for one textbox
CHill60 7-Mar-17 18:41pm    
Er...no. It searches for the contents of UP to 3 textboxes. Search, 2 and 3. It builds the Sql dynamically depending on the contents of three textboxes. Unlike many, I did test this before I posted it

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