Click here to Skip to main content
16,019,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code. Please help me as this code is not helping me.

.aspx code :

ASP.NET
<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:

C#
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");
        //ddlSearchItems.DataSource = ds.Tables[0].Columns;
        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;
        }
    }
}
Posted
Updated 15-Mar-15 20:46pm
v2
Comments
Maciej Los 16-Mar-15 2:50am    
Does above code return any error? Which line?

Hi,

Ref this below links..

1) Search and Filter GridView as you type in TextBox using jQuery AJAX in ASP.Net
2) Asp.net filter gridview records with dropdownlist selection
3) Filter ASP.Net GridView using DropDownList
4) Search Records In GridView And Highlight Results Using ASP.NET


Dynamically add column in Gridview..

C#
BoundField test = new BoundField();
test.DataField = "New DATAfield Name";
test.Headertext = "New Header";
GridViewId.Columns.Add(test);
 
Share this answer
 
v2
column names does not appear in the drop down list and the search text box also does not work
 
Share this answer
 
Comments
[no name] 16-Mar-15 4:18am    
Check the above reference..
Member 11304660 16-Mar-15 4:31am    
i am not getting how to add column names from table into the dropdown list
Member 11304660 16-Mar-15 4:56am    
This link is not satisfying my requirement. The data of one column has been added to the dropdown list. But i want to add the column names
[no name] 16-Mar-15 4:58am    
You wanted to add column name dynamically?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900