SQL Command to Create a needed table in SQL Server
create table tabCandRegistration
(
CandName varchar(20),
DateofBirth date,
Age int,
JoinDate date,
EmailID varchar(20),
[Password] varchar(10)
)
Stored Procedure needs to be created
create procedure sp_GetAllCandidateRecords
as
begin
select CandName,EmailID,DateOfBirth as DOB,Age,JoinDate
from tabCandRegistration
end
create procedure sp_CheckUserWithCredentials
@e varchar(20),
@p varchar(10)
as
begin
select CandName from tabCandRegistration
where EmailID=@e and Password=@p
end
LoginUsingCookies.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginUsingCookies.aspx.cs" Inherits="WebDemo01.LoginUsingCookies" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>My
Trainees</title>
</head>
<body style="background-color:deepskyblue">
<form id="form1" runat="server">
<div>
<h1>.NET
Training Program</h1>
Welcome to Sharp Land Software
Training Division<br /><br />
<table>
<tr>
<td>Enter Email ID</td>
<td>
<asp:TextBox ID="txtEmailID" runat="server"> </asp:TextBox>
</td>
</tr>
<tr>
<td>Enter Password</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"> </asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click"
Width="80px" />
<asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click"
Width="80px" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" ForeColor="Red"
Text="Login
module uses cookie">
</asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
LoginUsingCookies.aspx.cs
using System;
//Added..
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
//To use
cookie
using System.Web;
namespace WebDemo01
{
public partial class LoginUsingCookies : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
//Step1
(Connection estab)
string conStr = ConfigurationManager
.ConnectionStrings["SampleDBConStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
con.Open();
//step2
(Initialize command object with query and parameters)
SqlCommand cmd = new SqlCommand
("sp_CheckUserWithCredentials", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@e", txtEmailID.Text);
cmd.Parameters.AddWithValue("@p", txtPassword.Text);
//Step3 check
needed record available or not in DB?
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string candName = dr["CandName"].ToString();
lblMsg.Text = "Welcome...." + candName;
HttpCookie cookie = new HttpCookie("UserDetails");
cookie.Value = candName;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
//Delayed
redirection
Response.AppendHeader
("Refresh", "3;url=RecordDisplayUsingCookies.aspx");
}
else
{
lblMsg.Text = "User not found....";
}
//closer steps
cmd.Dispose();
con.Close();
}
protected void
btnReset_Click(object sender, EventArgs e)
{
txtEmailID.Text = "";
txtPassword.Text = "";
lblMsg.Text = "Enter user credentials.";
txtEmailID.Focus();
}
}
}
RecordDisplayUsingCookies.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RecordDisplayUsingCookies.aspx.cs" Inherits="WebDemo01.RecordDisplayUsingCookies" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<h3>Candidate Records</h3>
</td>
<td style="width:200px;">
<asp:Label ID="lblStatus"
runat="server" Text="Hello
user..."></asp:Label>
</td>
<td>
<asp:LinkButton ID="lnkbtnSignOut" runat="server" OnClick="lnkbtnSignOut_Click">Sign Out
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server"
Width="600px" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CandName"
HeaderText="Name"
ItemStyle-BackColor="#009933"
ItemStyle-ForeColor="White" />
<asp:BoundField DataField="DOB"
HeaderText="DOB"
DataFormatString="{0:dd/MM/yyyy}"
ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField DataField="Age"
HeaderText="Age"
DataFormatString = "{0} years"/>
<asp:BoundField DataField="JoinDate"
HeaderText="JoinedOn"
DataFormatString = "{0:dd/MM/yyyy}"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="EmailID"
HeaderText="Email" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
RecordDisplayUsingCookies.aspx.cs
using System;
//Added...
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
//To
access HttpCookieCollection
using System.Web;
namespace WebDemo01
{
public partial class RecordDisplayUsingCookies : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
HttpCookieCollection MyCookieCollection = Request.Cookies;
HttpCookie MyCookie = MyCookieCollection.Get("UserDetails");
if (MyCookie!=null)
{
lblStatus.Text = "Welcome " +
MyCookie.Value;
DisplayCandidateRecords();
}
else
{
lblStatus.Text = "You have not logged in...";
}
}
private void
DisplayCandidateRecords()
{
//Step1
(Connection estab)
string conStr = ConfigurationManager
.ConnectionStrings["SampleDBConStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
con.Open();
//step2
(Initialize command object with a query)
SqlCommand cmd = new SqlCommand
("sp_GetAllCandidateRecords", con);
cmd.CommandType = CommandType.StoredProcedure;
//Step3
(Initialize adapter with cmd object)
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Step4: Fill
dataset
DataSet ds = new DataSet();
da.Fill(ds);
//Step5 :
populate GridView with records...
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void
lnkbtnSignOut_Click(object sender, EventArgs e)
{
Response.Cookies["UserDetails"].Expires = DateTime.Now.AddDays(-1);
lblStatus.Text = "You are Logged out";
Response.AppendHeader
("Refresh", "3;url=LoginUsingCookies.aspx");
}
}
}
No comments:
Post a Comment