Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to search an employee from a database and display the results on a grid
Posted
Comments
OriginalGriff 19-Oct-12 5:55am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 19-Oct-12 5:56am    
this employee is a certain person name or a worksheet name?
[no name] 19-Oct-12 5:59am    
You open a connection to the database, query the information you want, and display it.
V. 19-Oct-12 6:11am    
1. your post title is meaningless
2. what database are you working with?
3. Is it winforms, wpf or Asp.Net?
4. Where are you stuck exactly, have you tried anything yet or do you have trouble finding a starting point?

1 solution

Enter EmployeeName in Textbox and Click on search Button and then do coding for Bind Gridview using Asp.net

XML
<div>
        Enter first name to search:
    </div>
    <div>
        <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged">
        </asp:TextBox>&nbsp;
    </div>
    <div>
        <asp:UpdatePanel ID="updatepanel1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="Employee Id">
                            <ItemTemplate>
                                <asp:Label ID="lblFirstName" runat="server" Text='<%# Highlight(Eval("emp_id").ToString()) %>'>
                                </asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Employee First Name">
                            <ItemTemplate>
                                <asp:Label ID="lblLastName" runat="server" Text='<%#(Eval("emp_first_name")) %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Employee Middle Name">
                            <ItemTemplate>
                                <asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_middle_name")) %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Employee Last Name">
                            <ItemTemplate>
                                <asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_last_name")) %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Employee Name">
                            <ItemTemplate>
                                <asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_name")) %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="txtSearch" EventName="TextChanged" />
            </Triggers>
        </asp:UpdatePanel>
    </div>



.CS file
string strConnection = "Data Source=BITPLUS3\\SqlExpress;Initial Catalog=HRDB;User ID=sa;pwd=bit123";

        //ConfigurationManager.AppSettings["WebHRConnectionString"];

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
         //   BindGrid();
        }

    }

    private DataTable GetRecords()
    {
        SqlConnection conn = new SqlConnection(strConnection);
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select emp_id, emp_first_name, emp_middle_name,emp_last_name, emp_name from tblemployee";
 
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        DataSet objDs = new DataSet();
        dAdapter.Fill(objDs);
        return objDs.Tables[0];

    }
    private void BindGrid()
    {
        DataTable dt = GetRecords();
        if (dt.Rows.Count > 0)
        {
            grdSearch.DataSource = dt;
            grdSearch.DataBind();
        }
    }
    private void SearchText()
    {
        DataTable dt = GetRecords();
        DataView dv = new DataView(dt);
        string SearchExpression = null;
        if (!String.IsNullOrEmpty(txtSearch.Text))
        {
            SearchExpression = string.Format("{0} '%{1}%'",
            grdSearch.SortExpression, txtSearch.Text);

        }
 
        dv.RowFilter = "emp_name like" + SearchExpression;
        grdSearch.DataSource = dv;
        grdSearch.DataBind();

    }
    public string Highlight(string InputTxt)
    {
        string Search_Str = txtSearch.Text.ToString();
        // Setup the regular expression and add the Or operator.
        Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
        RegexOptions.IgnoreCase);

        // Highlight keywords by calling the 
        //delegate each time a keyword is found.
        return RegExp.Replace(InputTxt,
        new MatchEvaluator(ReplaceKeyWords));

        // Set the RegExp to null.
        RegExp = null;

    }

    public string ReplaceKeyWords(Match m)
    {

        return "<span class=highlight>" + m.Value + "</span>";

    }

    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
 
        SearchText();
    }
 
Share this answer
 

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