Click here to Skip to main content
14,930,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void txt_search_company_TextChanged(object sender, EventArgs e)
            SqlDataAdapter adapt = new SqlDataAdapter("select * from tbl_company where c_name like '" + txt_search_company.Text + "%'", con.mycon);
            DataTable dt = new DataTable();
            dataGridView1.DataSource = dt;

This is the above code is only search the record.
But i find not record found display data.
Updated 26-Jul-15 1:54am

1 solution

One probable cause not finding the correct data is that you concatenate the value from your text box directly to the SQL statement. Never do this since it leaves you open to SQL injections. Consider what happens if your text box contains ' character. Instead use SqlParameter[^].

Also you probably want to add % in the end of the search string to be able to search with partial text only.

So your code should be something like
SqlCommand selectCommand = new SqlCommand("select * from tbl_company where c_name like @name + '%'", con.mycon)
selectCommand.Parameters.AddWithValue("@name", txt_search_company.Text)
SqlDataAdapter adapt = new SqlDataAdapter();
adapt.SelectCommand = selectCommand;
DataTable dt = new DataTable();
dataGridView1.DataSource = dt;

And if you need to investigate if any rows are returned, simply check the amount of dt.Rows after Fill.
if (dt.Rows.Count == 0) {
   // show some message to the user

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