Search in Grid View ASP.NET C#

(1)Initially  Design the Page like this in ASP.NET C#

Add this code in aspx Page :
<table border="0" cellpadding="0" cellspacing="0" width="60%" align="center">
            <tr>
                <td width="15%">
                    Serach By
                </td>
                <td width="10%">
                    <asp:DropDownList ID="ddlsearcby" runat="server"  Width="185px">
                    </asp:DropDownList>
                   
                </td>
                <td> <asp:TextBox ID="txtsearch" runat="server" /></td>
                <td width="5%">
                    <asp:Button ID="btnSearch" runat="server" Text="Search" 
                        onclick="btnSearch_Click" />
                </td>
            </tr>
            <tr>
            <td colspan="4"><hr /></td>
            </tr>
            <tr>
                <td colspan="4">
                    <asp:GridView ID="gdvdata" runat="server" Width="100%" >
                     <AlternatingRowStyle BackColor="AliceBlue" ForeColor="#284775" />  
                    </asp:GridView>
                </td>
            </tr>
        </table>

(2)Create Procedure in sql server:

create proc sp_serachEmployeeRecord 
@TypeOfSearch varchar(50),
@Value varchar(600)
as
begin
declare @quary nvarchar(max),@cnt int
select @quary=
case @TypeOfSearch when 'EMployeeID' then 'select top 50 T184001,T184002,T184003 from T184 where T184001='+@Value+''
when 'EmplyeName' then 'select top 50 T184001,T184002,T184003 from T184 where T184002 like'''+@Value+'%'''
when 'Gender' then 'select top 50 T184001,T184002,T184003 from T184 where T184003 like'''+@Value+'%'''
else 
'select top 50 T184001,T184002,T184003 from T184'
end
                                
exec sp_executesql @quary  
end

(3)Add this code in Code Behind  aspx.cs file in Asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class GrridDataSearch : System.Web.UI.Page
{
    public static SqlConnection con = new SqlConnection("Data Source=Your server name;Initial Catalog=Your DatabaseName;uid=sa; pwd=Password1");
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                Bindgrid();
                BindDropDownList();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            Bindgrid();
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }
    private void Bindgrid()
    {

        using (SqlCommand cmd = new SqlCommand("sp_serachEmployeeRecord", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TypeOfSearch",ddlsearcby.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@Value",txtsearch.Text);
            con.Open();
            using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    if (dt == null) return;
                    ad.Fill(dt);
                    con.Close();
                    gdvdata.DataSource = dt;
                    gdvdata.DataBind();
                }

            }
        }
       
    }

    private void BindDropDownList()
    {
        DataTable _dt = new DataTable();
        _dt.Columns.Add("txt");
        _dt.Columns.Add("val");
        _dt.Rows.Add("Select", "Select");
        _dt.Rows.Add("EMployeeID", "EMployeeID");
        _dt.Rows.Add("EmplyeName", "EmplyeName");
        _dt.Rows.Add("Gender", "Gender");
        _dt.Rows.Add("All", "All");
        ddlsearcby.DataSource = _dt;
        ddlsearcby.DataTextField = "txt";
        ddlsearcby.DataValueField = "val";
        ddlsearcby.DataBind();

    }

}
Output:



Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment