Nomfundo Cindi
Nomfundo Cindi

Reputation: 11

Seach data by multiple text box or one by one in gridview in asp.net c#

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 temblate and bind with dataset. please tell me how to serach 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 suggession for asp.net 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();        
}

Upvotes: 1

Views: 989

Answers (1)

Techno Crave
Techno Crave

Reputation: 433

Try this..

Remove 3rd and 4th line and chage the query as below.

TextBox1.text is the Id 1st search textbox and TextBox2.text is the Id of 2nd search textbox

string str = "Select [ITEM No#], [Company Name], [Discipline Required], [Service Description], Institution, [Award Date] from PSP_Report where ([ITEM NO#] like '%" + TextBox1.Text == string.Empty ? null : TextBox1.Text + "%' OR [Company Name] like '%" + TextBox2.Text==string.Empty?null:TextBox2.Text + "%')";

Upvotes: 1

Related Questions