This is my code. Please help me as this code is not helping me.
.aspx code :
<asp:DropDownList ID="ddlSearchItems" runat="server">
<asp:TextBox ID="txtSearch" runat="server">
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"/>
<asp:BoundField DataField="LastName" HeaderText="LastName"/>
<asp:BoundField DataField="Sources" HeaderText="Sources" />
</columns>
.aspx.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Report2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindSearchTerm();
}
}
string query = "SELECT ID,FirstName,LastName FROM tblPersonalDetails";
protected void Search(object sender, EventArgs e)
{
string constr = @"Data Source=Vrushali-pc;Initial Catalog=HRRecruitment;Persist Security Info=True;User ID=sa;Password=hrrecruitment";
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()
{
DataTable dt = new DataTable();
string constr = @"Data Source=Vrushali-pc;Initial Catalog=HRRecruitment;Persist Security Info=True;User ID=sa;Password=hrrecruitment";
this.ddlSearchItems.DataSource = this.GetData(constr, "SELECT ID, FirstName+','+LastName+','+Sources AS Name FROM tblPersonalDetails");
for (int i = 0; i < dt.Columns.Count; i++)
{
ddlSearchItems.Items.Add(new ListItem(dt.Columns[i].ColumnName));
}
this.ddlSearchItems.DataSource = this.GetData(constr, "SELECT ID, FirstName+','+LastName+','+Sources AS Name FROM tblPersonalDetails");
this.ddlSearchItems.DataTextField = "Name";
this.ddlSearchItems.DataValueField = "ID";
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;
}
}
}