Hi parthiban,
Try the following...
<asp:DropDownList ID="ddlSearchItems" runat="server">
</asp:DropDownList>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" />
<asp:BoundField DataField="FirstName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindSearchTerm();
}
}
string query = "SELECT EmployeeId,FirstName,City,Country FROM Employees";
protected void Search(object sender, EventArgs e)
{
string constr = @"Data Source=192.168.0.1\SQL2005;Initial Catalog=Northwind;User=sa;Password=pass@123";
query += string.Format(" WHERE {0} = '{1}'", this.ddlSearchItems.SelectedItem.Text.Trim(), this.txtSearch.Text.Trim());
this.gvEmployees.DataSource = this.GetData(constr, query);
this.gvEmployees.DataBind();
}
private void BindSearchTerm()
{
string constr = @"Data Source=192.168.0.1\SQL2005;Initial Catalog=Sample;User=sa;Password=pass@123";
this.ddlSearchItems.DataSource = this.GetData(constr, "SELECT Name FROM SearchTerm");
this.ddlSearchItems.DataTextField = "Name";
this.ddlSearchItems.DataValueField = "Name";
this.ddlSearchItems.DataBind();
}
private DataTable GetData(string constr, string query)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
Namespaces
using System;
using System.Data;
using System.Data.SqlClient;