Create Login With Role Using Thee Tire Architecture In Asp.Net C#



1) Create table

CREATE TABLE  `dbname`.`login` (
  `id` int(11) NOT NULL auto_increment,
  `userid` varchar(50) default NULL,
  `password` varchar(50) default NULL,
  `Role` varchar(200) default NULL,
  KEY `id` (`id`)
)

2) Insert Values into table

ID USER-ID PASSWORD ROLE
1 Admin Admin@123456 Admin
2 Pratibha pratibha@123 Agent
3 Rohan rohan@123 Agent
4 User User@123 Customer





3)Create Prcedure , I am using mysql you can use mssql or mysql.

DELIMITER $$
DROP PROCEDURE IF EXISTS `dbname`.`login` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `login`()
BEGIN
select * from login;
END $$
DELIMITER ;


4) Create cs file in DAL (Data Access Layer) Paste Below code.

MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
      DataTable dt = new DataTable();
      public DataTable admin_loginDL()
      {
          try
          {
              MySqlCommand cmd = new MySqlCommand("login", con);
              cmd.CommandType = CommandType.StoredProcedure;

              using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
              {
                  sda.Fill(dt);
                  return dt;
              }
          }
          catch { throw; }
      }

5) Create cs file in BAL (Business Access Layer) Paste below code

public DataTable Admin_loginBL()
       {
           try
           {
              return objdl.admin_loginDL();
           }
           catch { throw; }
       }

6) Create Login page name as login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="admin_login" %>

<!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 id="Head1" runat="server">
    <title>Log in</title>
    <link rel='stylesheet prefetch' href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'>
    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <form id="form1" runat="server">
    <!-- Form Mixin-->
    <!-- Input Mixin-->
    <!-- Button Mixin-->
    <!-- Pen Title-->
    <div class="pen-title">
        <h1>
            Login</h1>
    </div>
    <!-- Form Module-->
    <div class="module form-module">
        <div class="toggle">
            <i class="fa fa-times fa-pencil"></i>
        </div>
        <div class="form">
            <h2>
                Login to your account</h2>
            <form>
            <asp:TextBox ID="txt_userid" placeholder="UserID" runat="server" required></asp:TextBox>
            <asp:TextBox ID="txt_password" TextMode="Password" placeholder="Password" runat="server"
                required></asp:TextBox>
            <asp:Button CssClass="submitBtn" ID="btn_Submit" runat="server" Text="LogIn" OnClick="btn_Submit_Click" />
            <asp:Label Style="color: Red;" ID="lblinfo" runat="server" Text=""></asp:Label>
            </form>
        </div>
        <div class="cta">
            <a href="#">Forgot your password?</a></div>
    </div>
    <script language="javascript" src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src="../js/index.js" type="text/javascript"></script>
    </form>
</body>
</html>




7) Paste code in login.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using System.Data;

public partial class admin_login : System.Web.UI.Page
{
   
    allBL objbl = new allBL();
    int RowCount;
    string UserName, Password;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = objbl.Admin_loginBL();

        RowCount = dt.Rows.Count;
            for (int i = 0; i < RowCount; i++)            
            {
                 UserName = dt.Rows[i]["userid"].ToString();
                Password = dt.Rows[i]["password"].ToString();
               

                    if (UserName == txt_userid.Text && Password == txt_password.Text)
                    {
                        if (dt.Rows[i]["Role"].ToString() == "Admin")
                        {
                            Session["username"] = txt_userid.Text;
                            Response.Redirect("index.aspx");
                        }
                       
                    }
                    else
                    {
                        lblinfo.Text = "wrong userid and password";
      
                    }
                    }

      
    }
}


OUTPUT


Comments

Popular posts from this blog

Disable right click, Inspect Element and page source using JavaScript in Html and asp.net