Introduction
In this lesson i explained about how to make registation and login with logout in asp.net using 3 layer achitecture.I have done asp validation for all the fields.
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
namespace BAL
{
public class balLogin
{
dalLogin dallogin = new dalLogin();
public int ballog(string userid, string pass)//checking the usename and password
{
try
{
int a = dallogin.userlogin(userid, pass);
string b = dallogin.userlog(userid, pass);
if (a == 1 && (b == "Level1" || b == "Level2" || b == "Level3" ))
{
return 1;
}
else if (a == 1 && b == "admin")
{
return 2;
}
else
{
return 0;
}
}
catch (Exception ec)
{
ec.GetType();
}
return 0;
}
public string usertype(string user, string pass)//checking the userType
{
try
{
return dallogin.userlog(user, pass);
}
catch(Exception ex)
{
throw ex;
}
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BO;
namespace DAL
{
public class dalRegister
{
SqlConnection conn = new SqlConnection(@"data source=LAKSHANYA\SQLEXPRESS;database=master;integrated security=true");
public int dalreg(boRegister boregister)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into user_table values('" + boregister.userName + "','" + boregister.gender + "','" + boregister.password + "','" + boregister.cPassword + "','" + boregister.fees + "','" + boregister.userType + "','" + boregister.eMail + "')", conn);
cmd.ExecuteNonQuery();
return 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class dalLogin
{
SqlConnection con = new SqlConnection(@"data source=LAKSHANYA\SQLEXPRESS;database=master;integrated security=true");
public int userlogin(string user, string passw)//checking the user name and password.If matched count 1 not 0.
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from user_table where userName='" + user + "'and password='" + passw + "'", con);
int a = Convert.ToInt32(cmd.ExecuteScalar());
return a;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
public string userlog(string user, string passw)//getting the userType
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select userType from user_table where userName='" + user + "' and password='" + passw + "'", con);
string a = cmd.ExecuteScalar().ToString();
return a;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
|
Note
For logout i did that in master page. so that logout option will come in all the pages.
|
Include this in your master page
<div class="loginDisplay">
Welcome
<asp:Label ID="lblUser" runat="server"></asp:Label>
<asp:Button ID="Sign_out" runat="server" onclick="Sign_out_Click"
Text="Signout" />
</div>
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace cdManage
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
lblUser.Text = Session["userName"].ToString();
}
protected void Sign_out_Click(object sender, EventArgs e)
{
Session.Remove("userName");
Response.Redirect("Home.aspx");
}
}
}
|
Look of this logout After logged in
|
Conclusion
I think my lesson helped you.Thanks for reading it.
|
Preview of page
|
Note
I have created seperate notes for registration page and also for login.If you get any clarification see that lessons.I have explained deeply in that notes.In this lesson i gave the codes with combination of registration and login operations.
Registration page click here For login page click here.
|
Solution expolrer
Note : Dont forget to refer the class library.Because i have done this in 3 layer architecture
Inside BAL class library i have created class named balRegister.cs and balLogin.cs
Inside BO class library i have created class named boRegister.cs
Inside DAL class library i have created class named dalRegister.cs dalLogin.cs
Inside cdManage application i have created web form named Home.aspx
Below i gave code for all the classes and web form.
|
create table user_table(
userName varchar(20),
gender varchar(20),
password varchar(20),
cpassword varchar(20),
fees varchar(20),
userType varchar(20),
email varchar(50))
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 436px;
text-align: center;
height: 45px;
}
.style2
{
color: #0066CC;
text-align: center;
}
.style7
{
height: 45px;
color: #0066CC;
text-align: left;
}
.style8
{
height: 45px;
color: #0066CC;
text-align: center;
}
.style9
{
height: 45px;
color: #0066CC;
width: 256px;
text-align: center;
}
.style10
{
width: 237px;
text-align: center;
height: 26px;
}
.style11
{
width: 225px;
text-align: center;
height: 26px;
}
.style12
{
color: #0066CC;
width: 256px;
text-align: center;
}
.style13
{
color: #0066CC;
text-align: left;
}
.style14
{
width: 436px;
text-align: center;
height: 30px;
}
.style15
{
height: 30px;
color: #0066CC;
width: 256px;
text-align: center;
}
.style16
{
height: 30px;
color: #0066CC;
text-align: left;
}
.style17
{
width: 436px;
text-align: center;
height: 38px;
}
</style>
</head>
<body>
<form id="Form1" runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
GK CdS Ltd.
</h1>
</div>
<div class="loginDisplay">
</div>
<div class="clear hideSkiplink">
</div>
</div>
<script type="text/javascript" >
function do_highlight(ele) {
document.getElementById(ele.id).style.color = "black";
if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username") {
document.getElementById(ele.id).value = "";
}
if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password") {
document.getElementById(ele.id).value = "";
}
}
function do_unfocus(ele) {
if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "") {
document.getElementById(ele.id).value = "Username";
}
if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "") {
document.getElementById(ele.id).value = "Password";
}
if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username")
document.getElementById(ele.id).style.color = "rgb(70,70,70)";
if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password")
document.getElementById(ele.id).style.color = "rgb(70,70,70)";
}
</script>
<div class="clear">
<br />
<h2><centre>Welcome to GK CdS Ltd</centre></h2><br />
<br />
<br />
<br />
<br />
<table style="width:100%;">
<tr>
<td style="color: #0033CC" class="style1" colspan="2">
<h3 class="style2"><strong> Members Login Here</strong></h3></td>
<td colspan="2">
<h3 class="style2"> <strong>New Member Sign Up Here</strong></h3></td>
</tr>
<tr>
<td class="style14" colspan="2">
<asp:TextBox ID="UserName" runat="server" onfocus="do_highlight(this);"
onblur="do_unfocus(this);" value="Username"
MaxLength="20" ForeColor="#707070"></asp:TextBox>
<asp:TextBox ID="PassLog" runat="server" onfocus="do_highlight(this);"
onblur="do_unfocus(this);" value="Password" ForeColor="#707070"
MaxLength="10" TextMode="Password"></asp:TextBox>
</td>
<td class="style15">
User Name</td>
<td class="style16">
<asp:TextBox ID="Uname" runat="server" MaxLength="15"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="re_user" runat="server"
ControlToValidate="Uname" Display="Dynamic"
ErrorMessage="Please enter the User Name" ForeColor="Red"
ValidationGroup="signUp"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style11">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="UserName" ErrorMessage="Please Enter the User Name"
ForeColor="Red" InitialValue="Username" ValidationGroup="login_gr"></asp:RequiredFieldValidator>
</td>
<td class="style10">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="PassLog" ErrorMessage="Please Enter the Password"
ForeColor="Red" InitialValue="Password" ValidationGroup="login_gr"></asp:RequiredFieldValidator>
</td>
<td class="style12" rowspan="2">
Gender</td>
<td class="style13" rowspan="2">
<asp:RadioButtonList ID="Gender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="re_gender" runat="server"
ControlToValidate="Gender" Display="Dynamic"
ErrorMessage="Please Select the Gender" ForeColor="Red"
ValidationGroup="signUp"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style17" colspan="2">
<asp:Button ID="Login" class="button_example" runat="server" Text="Login"
onclick="Login_Click" ValidationGroup="login_gr" Height="38px" />
</td>
</tr>
<tr>
<td class="style1" colspan="2">
<asp:Label ID="log_mess" runat="server"></asp:Label>
</td>
<td class="style9">
Password</td>
<td class="style7">
<asp:TextBox ID="Password" runat="server" MaxLength="8" TextMode="Password"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="re_pass" runat="server"
ControlToValidate="Password" Display="Dynamic"
ErrorMessage="Please Enter the Password" ForeColor="Red"
ValidationGroup="signUp"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1" colspan="2">
</td>
<td class="style9">
Confirm Password</td>
<td class="style7">
<asp:TextBox ID="Cpassword" runat="server" MaxLength="8" TextMode="Password"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="re_cpass" runat="server"
ControlToValidate="Cpassword" Display="Dynamic"
ErrorMessage="Please Enter the Confirm Password" ForeColor="Red"
ValidationGroup="signUp"></asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="re_passCompare" runat="server"
ControlToCompare="Password" ControlToValidate="Cpassword" Display="Dynamic"
ErrorMessage="Password Mismatch" ForeColor="Red" ValidationGroup="signUp"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style1" colspan="2">
</td>
<td class="style9">
Fees</td>
<td class="style7">
<asp:DropDownList ID="Fees" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>299</asp:ListItem>
<asp:ListItem>249</asp:ListItem>
<asp:ListItem>Free User</asp:ListItem>
</asp:DropDownList>
<br />
<asp:RequiredFieldValidator ID="re_fees" runat="server"
ControlToValidate="Fees" ErrorMessage="Please Select the Fees" ForeColor="Red"
ValidationGroup="signUp"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1" colspan="2">
</td>
<td class="style9">
E-mail Id</td>
<td class="style7">
<asp:TextBox ID="eMail" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="re_email" runat="server"
ControlToValidate="eMail" Display="Dynamic"
ErrorMessage="Please Enter the Email" ForeColor="Red" ValidationGroup="signUp"></asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="re_validemail" runat="server"
ControlToValidate="eMail" Display="Dynamic"
ErrorMessage="Enter the Valid Email Id" ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="signUp"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style1" colspan="2">
</td>
<td class="style8" colspan="2">
<asp:Button ID="Button1" class="button_example" runat="server" Text="Sign Up"
onclick="Button1_Click" ValidationGroup="signUp"
/>
</td>
</tr>
<tr>
<td class="style1" colspan="2">
</td>
<td class="style8" colspan="2">
<asp:Label runat="server" ID="message"></asp:Label>
</td>
</tr>
</table>
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using BAL;
namespace cdManage
{
public partial class Home : System.Web.UI.Page
{
boRegister register = new boRegister();
balRegister balregister = new balRegister();
balLogin ballogin = new balLogin();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
register.userName = Uname.Text;
register.gender = Gender.SelectedValue;
register.password = Password.Text;
register.cPassword = Cpassword.Text;
register.fees = Fees.SelectedValue;
register.eMail = eMail.Text;
if (Fees.SelectedIndex ==1)
register.userType = "Level1";
if (Fees.SelectedIndex == 2)
register.userType = "Level2";
else if(Fees.SelectedIndex == 3)
register.userType = "Level3";
int value= balregister.balreg(register);
Uname.Text = "";
Gender.ClearSelection();
Fees.SelectedIndex = 0;
eMail.Text = "";
if (value == 1)
{
message.Text = "Registered Successfully";
}
else
{
message.Text = "User Name Not Available.Try With Different User Name";
}
}
protected void Login_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
string password = PassLog.Text;
int a = ballogin.ballog(userName, password);
UserName.Text = "";
PassLog.Text = "";
if (a == 1)
{
Session["userName"] = userName;
string type = ballogin.usertype(userName, password);
Session["usertype"] = type;
Response.Redirect("userHome.aspx");
}
else if (a == 2)
{
Session["userName"] = userName;
Response.Redirect("adminHome.aspx");
}
else
{
log_mess.Text = "Enter the valid id";
}
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BO
{
public class boRegister
{
private string _userName;
public string userName
{
get { return _userName; }
set { _userName = value; }
}
private string _gender;
public string gender
{
get { return _gender; }
set { _gender = value; }
}
private string _fees;
public string fees
{
get { return _fees; }
set { _fees = value; }
}
private string _password;
public string password
{
get { return _password; }
set { _password = value; }
}
private string _cPassword;
public string cPassword
{
get { return _cPassword; }
set { _cPassword = value; }
}
private string _eMail;
public string eMail
{
get { return _eMail; }
set { _eMail = value; }
}
private string _usertype;
public string userType
{
get { return _usertype; }
set { _usertype = value; }
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using DAL;
namespace BAL
{
public class balRegister
{
dalRegister dalregister = new dalRegister();
public int balreg(boRegister boregister)
{
try
{
return dalregister.dalreg(boregister);
}
catch (Exception ex)
{
ex.GetType();
}
return 0;
}
}
}
|