Create Stored Procedure for Login | ASP.NET C# | Final Year Project | Login Type 3 | Coder Baba
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace CoderBabaSystem.wwwroot
{
public partial class Login : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Second way as shown below:This is the best way for best and fast performance
if (IsFormValid())
{
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("usp_CheckLoginDetails", con);
cmd.Parameters.AddWithValue("@UserName",txtUserName.Text);
cmd.Parameters.AddWithValue("@Password",txtPassword.Text);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
int i = 0;
i = cmd.ExecuteNonQuery();
con.Close();
//How to check Username and Password is matched or not
if (dt.Rows.Count > 0)
{
Session["user"] = txtUserName.Text;
Response.Write("<script>alert('Login Successful')</script>");
txtUserName.Text = string.Empty;
txtPassword.Text = string.Empty;
Response.Redirect("HomePage.aspx");
}
else
{
Response.Write("<script>alert('Login failed')</script>");
txtUserName.Text = string.Empty;
txtPassword.Text = string.Empty;
txtUserName.Focus();
}
con.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: ", ex.ToString());
Response.Write("<script>alert('exception Login Failed error')</script>");
Response.Write(ex.Message);
}
}
else
{
Response.Write("<script>alert('Validation fail error')</script>");
}
//First Way as shown below :
//if (IsFormValid())
//{
// try
// {
// SqlConnection con = new SqlConnection(cs);
// con.Open();
// SqlCommand cmd = new SqlCommand("SELECT * FROM tblUser with(nolock) WHERE UserName='" + txtUserName.Text + "' AND Password='" + txtPassword.Text + "'", con);
// SqlDataReader dr = cmd.ExecuteReader();
// //How to check Username and Password is matched or not
// if (dr.Read())
// {
// Response.Write("<script>alert('Login Successful')</script>");
// }
// else
// {
// Response.Write("<script>alert('Login failed')</script>");
// }
// con.Close();
// }
// catch (Exception ex)
// {
// Console.WriteLine("Exception: ", ex.ToString());
// }
//}
//else
//{
// Response.Write("<script>alert('Validation fail error')</script>");
//}
}
private bool IsFormValid()
{
string User = txtUserName.Text;
string Pass = txtPassword.Text;
//Condition No. 1
if (User == "" && Pass == "")
{
Response.Write("<script>alert('Username & Password are necessary ! Please enter & try again..')</script>");
return false;
}
else if (User == "")
{
Response.Write("<script>alert('Username necessary ! Please enter Username..')</script>");
return false;
}
else if (Pass == "")
{
Response.Write("<script>alert('Password necessary ! Please enter Password..')</script>");
return false;
}
else if (User.Length == 0)
{
Response.Write("<script>alert('Enter Username ! it can not be blank')</script>");
return false;
}
//Condition No. 2
else if (User.Length < 4)
{
Response.Write("<script>alert('Invalid Length.Username should be at least 4 character long.')</script>");
return false;
}
else if (Pass.Length < 4 || Pass.Length >= 18)
{
Response.Write("<script>alert('Invalid Length, Password should be at least 4 Character long & Max 18 Char..')</script>");
return false;
}
return true;
}
}
}
Above File is CoderBabaSystem\wwwroot\Login.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CoderBabaSystem.wwwroot.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Coder Baba</title>
<meta charset="UTF-8"/>
<meta name="description" content="Free Web tutorials"/>
<meta name="keywords" content="HTML,CSS,XML,JavaScript"/>
<meta name="author" content="John Doe"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="CSS/Header.css" rel="stylesheet" type="text/css"/>
<link href="CSS/footer.css" rel="stylesheet" type="text/css"/>
<link href="CSS/LoginCSS.css" rel="stylesheet" type="text/css"/>
<script language="javascript" type="text/javascript">
function ValidateForm()
{
var User = document.getElementById("txtUserName").value;
var Pass = document.getElementById("txtPassword").value;
//Condition No. 1
if (User == "" && Pass == "") {
alert("Username & Password are necessary ! Please enter & try again..");
document.getElementById("txtUserName").focus();
return false;
}
else if (User == "") {
alert("Username necessary ! Please enter Username..");
document.getElementById("txtUserName").focus();
return false;
}
else if (Pass == "") {
alert("Password necessary ! Please enter Password..");
document.getElementById("txtPassword").focus();
return false;
}
else if (User.length == 0)
{
alert("Enter Username ! it can not be blank");
document.getElementById("txtUserName").focus();
return false;
}
//Condition No. 2
else if (User.length < 4) {
alert("Invalid Length.Username should be at least 4 character long.");
document.getElementById("txtUserName").focus();
return false;
}
else if (Pass.length < 4 || Pass.length >= 18)
{
alert("Invalid Length, Password should be at least 4 Character long & Max 18 Char..");
document.getElementById("txtPassword").focus();
return false;
}
return true;
//alert("Hello you click login button");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<%--This is Header Section--%>
<header>
<div class="wrapper">
<h1>CODERBABA WEBSITE</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Blog</a></li>
</ul>
</nav>
</div>
</header>
<%--Header Section End here--%>
<div style="width:700px;margin-left:350px;margin-top:50px;">
<h2 style="text-align:center;background-color:lightgreen;padding:10px;">Login Form</h2>
</div>
<div class="imgcontainer">
<img src="images/apple.png" width="100" height="100" alt="Avatar" class="avatar"/>
</div>
<div class="container">
<label for="uname"><b>UserName</b></label>
<asp:TextBox ID="txtUserName" runat="server" placeholder="Enter Username"></asp:TextBox>
<label for="psw"><b>Password</b></label>
<asp:TextBox ID="txtPassword" runat="server" placeholder="Enter Password" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Login" type="submit" Font-Size="Larger" OnClick="Button1_Click" OnClientClick="return ValidateForm123();"/>
<label>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="Larger" Text="Remember Me"/>
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot<a href="#">Password?</a></span>
</div>
<br/>
<footer>
<div class="wrapper">
<div id="footer-info">
<p>Copyright@2020 CODERBABA.in All rights reserved</p>
<p><a href="#">Terms of Services</a> || <a href="#">Privacy</a></p>
</div>
<div id="footer-links">
<ul>
<li><h5>Home</h5></li>
<li><a href="#">About us</a></li>
<li><a href="#">Meet the Team</a></li>
<li><a href="#">What we do?</a></li>
<li><a href="#">Careers</a></li>
</ul>
<ul>
<li><h5>Company</h5></li>
<li><a href="#">About us</a></li>
<li><a href="#">Meet the Team</a></li>
<li><a href="#">What we do?</a></li>
<li><a href="#">Careers</a></li>
</ul>
</div>
<div class="clear"></div>
</div>
</footer>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment