Simple Search Functionality

Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 1047
Firstly In aspx page add textbox,Butoon to search and grid view to display records.
 <asp:TextBox ID="txtSEarch" runat="server"></asp:TextBox>
<asp:Button ID="btnSerach" runat="server" Text="Search" OnClick="SearchData" />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

In Code Behind:
Name spaces Used are:
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

 string _connstr = ConfigurationManager.ConnectionStrings["Connstr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)

protected void SearchData(object sender, EventArgs e)
DataTable table = new DataTable();

using (SqlConnection conn = new SqlConnection(_connstr))
string SQL = "SELECT * FROM PersonDetails2 WHERE FirstName like '%" + (txtSEarch.Text) + "%'";
using (SqlCommand cmd = new SqlCommand(SQL, conn))

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "PersonDetails2");
GridView1.DataSource = ds;


Thanks & Regards

Comments or Responses

Login to post response