<asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlColumns" runat="server" AutoPostBack = "true">
<asp:ListItem Text="All" Value="All"></asp:ListItem>
<asp:ListItem Text="City" Value="City"></asp:ListItem>
<asp:ListItem Text="Country" Value="Country"></asp:ListItem>
</asp:DropDownList><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:BoundField DataField = "CustomerId" HeaderText = "Customer Id" />
<asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" />
<asp:BoundField DataField = "City" HeaderText = "City" />
<asp:BoundField DataField = "Country" HeaderText = "Country" />
</Columns>
</asp:GridView>
String conString = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string query = "select * from customers";
SqlCommand cmd = new SqlCommand();
if (ddlColumns.SelectedIndex > 0 && !string.IsNullOrEmpty(txtValue.Text.Trim()))
{
query = query + " where " + ddlColumns.SelectedItem.Value + "=@Param";
cmd.Parameters.AddWithValue("@Param", txtValue.Text.Trim());
}
cmd.CommandText = query;
SqlConnection con = new SqlConnection(conString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();