private void btnRetrieve_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtId.Text) || !string.IsNullOrEmpty(txtName.Text) || !string.IsNullOrEmpty(txtLocation.Text))
{
StringBuilder SB = new StringBuilder("select * from Employee where ");
if (!string.IsNullOrEmpty(txtId.Text))
{
SB.Append("Emp_id='" + txtId.Text + "'");
}
if (!string.IsNullOrEmpty(txtName.Text))
{
if (!string.IsNullOrEmpty(txtId.Text))
{
SB.Append(" and ");
}
SB.Append("Emp_name='" + txtName.Text + "'");
}
if (!string.IsNullOrEmpty(txtLocation.Text))
{
if (!string.IsNullOrEmpty(txtId.Text) || !string.IsNullOrEmpty(txtName.Text))
{
SB.Append(" and ");
}
SB.Append("Emp_location='" + txtLocation.Text + "'");
}
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(SB.ToString(), con);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
else
{
MessageBox.Show("Give atleast one data for search....!");
}
}This code will be very useful for who are using string builder for searching operation in the windows form application.
