create a database name log and run the following script
USE [log]
GO
/****** Object: Table [dbo].[Student_S] Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
[id] [int] IDENTITY(1,1) NOT NULL,
[Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED
(
[Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridsearch.aspx.cs" Inherits="gridsearch" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<h2 style="color: blue; font-style: normal">
Gridview Search in asp.net
<br />
</h2>
<div>
<asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="PSWD"></asp:Label>
<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Count"></asp:Label>
<asp:TextBox ID="txtcount" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" ForeColor="Blue" />
<br />
<asp:Label ID="Label4" runat="server" Visible="false"></asp:Label>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class gridsearch : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
Label4.Visible = false;
SqlCommand cmd = new SqlCommand("select * from login", con);
SqlDataAdapter sadp = new SqlDataAdapter(cmd);
sadp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
int flag = 0;
if (txtid.Text == "" && txtpwd.Text == "" && txtcount.Text == "")
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else if (txtid.Text != "" && txtpwd.Text == "" && txtcount.Text == "")
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (txtid.Text == ds.Tables[0].Rows[i][0].ToString())
{
flag = 1;
SqlCommand cmd1 = new SqlCommand("select id from login where id='" + txtid.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(dt);
break;
}
}
if (flag == 1)
{
GridView1.DataSource = dt;
GridView1.DataBind();
flag = 0;
}
else
{
Label4.Visible = true;
Label4.Text = "no such user id exists";
}
}
else if (txtid.Text != "" && txtpwd.Text != "" && txtcount.Text == "")
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (txtid.Text == ds.Tables[0].Rows[i][0].ToString() && txtpwd.Text == ds.Tables[0].Rows[i][2].ToString())
{
flag = 1;
SqlCommand cmd1 = new SqlCommand("select id,Password from login where id='" + txtid.Text + "' and Password='" + txtpwd.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(dt);
break;
}
}
if (flag == 1)
{
GridView1.DataSource = dt;
GridView1.DataBind();
flag = 0;
}
else
{
Label4.Visible = true;
Label4.Text = "invalid data";
}
}
else if (txtid.Text != "" && txtpwd.Text != "" && txtcount.Text != "")
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (txtid.Text == ds.Tables[0].Rows[i][0].ToString() && txtpwd.Text == ds.Tables[0].Rows[i][2].ToString() && txtcount.Text == ds.Tables[0].Rows[i][4].ToString())
{
flag = 1;
SqlCommand cmd1 = new SqlCommand("select UserName,Password,Email from login where id='" + txtid.Text + "' and Password='" + txtpwd.Text + "' and Roles='" + txtcount.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(dt);
break;
}
}
if (flag == 1)
{
GridView1.DataSource = dt;
GridView1.DataBind();
flag = 0;
}
else
{
Label4.Visible = true;
Label4.Text = "invalid data";
}
}
txtcount.Text = "";
txtid.Text = "";
txtpwd.Text = "";
}
}


No comments:
Post a Comment