indradeo says:
protected
void
btnSearch_Click(
object
sender, EventArgs e)
{
BindGridView(TextBox1.Text);
}
private
void
BindGridView(
string
searchVal)
{
SqlConnection con =
new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[
"constr"
].ToString());
try
{
DataTable objdt =
new
DataTable();
string
query = searchVal ==
""
?
"select * from tbl_Employees;"
:
"select * from tbl_Employees where EmpCode like '%"
+ searchVal +
"%';"
;
SqlDataAdapter da =
new
SqlDataAdapter(query, con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource = objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
Replace with below code.
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGridView(TextBoxSearch.Text);
}
private void BindGridView(string searchVal)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
try
{
DataTable objdt = new DataTable();
string query = "select * from tbl_Employees ";
if (!string.IsNullOrEmpty(searchVal))
{
query += " where EmpCode like '%" + searchVal + "%';";
}
SqlDataAdapter da = new SqlDataAdapter(query, con);
con.Open();
da.Fill(objdt);
con.Close();
if (objdt.Rows.Count > 0)
{
GridView1.DataSource = objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}