Introduction
The seach is important in the web site. When you are create a new website you have to implement in the search features as well. So in this article I will explain how to search record from database using gridview.
Implementation
Create page named search.aspx . Palce a textbox.linkbutton,label,gridview at this page. You can see it in the HTML code.here I’m using templatefields for bimd the gridview.
HTML code for search.aspx page
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style3">
Search Employee: </td>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Go</asp:LinkButton>
</td>
</tr>
<tr>
<td class="style3">
<h3>
<i><b <span class="style4">The Detail Of Employee </span></b></i></h3>
</td>
<td class="style2">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" Height="90px" Width="260px">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("ename") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%#Eval("eadd") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%#Eval("email") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="username">
<ItemTemplate>
<%#Eval("uname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<%#Eval("pwd") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<h5>
</h5>
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
C# code for search.aspx page
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;
using System.Configuration;
public partial class search : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
string b = "";
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
private void user_chk()
{
string a;
a = TextBox1.Text;
// SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select ename from tbemp where ename like'" + a + "%'";
cmd.Connection = con;
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
rep_bind();
GridView1.Visible = true;
TextBox1.Text = "";
Label1.Text = "";
}
else
{
GridView1.Visible = false;
b = TextBox1.Text + " Is Not Available in the List.";
Label1.Text = b;
TextBox1.Text = "";
}
}
private void rep_bind()
{
string a;
a = TextBox1.Text;
SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp where ename like'" + a + "%'", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
// here i'm calling function named user_chk() at this button click
user_chk();
}
}
Conclusion
Through this article, you have learned how we can create search using gridview.
Database Script
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbemp]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tbemp](
[id] [int] IDENTITY(1,1) NOT NULL,
[ename] [varchar](100) NULL,
[eadd] [varchar](500) NULL,
[email] [varchar](100) NULL,
[uname] [varchar](50) NULL,
[pwd] [varchar](50) NULL,
CONSTRAINT [PK_tbemp] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
Output of the program
Thanks for the sharing.. its really helpful for my asp.net site :)
ReplyDelete