Technologies
Asp.Net
Develop login page by using 3 Tier architecture in asp.net
  sridotnet
  Friday, 23 Jun 2017
  943
    0
Develop login page by using 3 Tier architecture in asp.net

In this articles i'm going to show you how to create login page with 3 tier architecture by using asp.net c# coding. First we need to design asp.net page as i shown in above images simply copy and paste the below html and css code for designing purpose.


Create Login Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="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 runat="server">
    <title></title>

---------   ADD CSS CODE HERE    ----------

</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:TextBox ID="txtusername" CssClass="textbox_username textbox" runat="server" placeholder="Username"></asp:TextBox><br /><br />
     <asp:TextBox ID="txtpassword" CssClass="textbox_pwd textbox" runat="server" placeholder="Password"></asp:TextBox><br /><br />
     <asp:Button ID="Btn_Login" CssClass="Button" runat="server" Text="Login" />
    </div>
    </form>
</body>
</html>

CSS Code


 
 <style type="text/css">
     .textbox_username
     {   
         background:url("Images/username.jpg") no-repeat;         
     }
     .textbox_pwd
     {
         background:url("Images/password.jpg") no-repeat;    
     }
     .textbox
     {         
         padding:7px 10px 7px 40px;         
         width:200px;         
         border:1px solid #dddddd;
         border-radius:5px;
     }
     .Button
     {
         padding:7px 20px 7px 20px;
         background:linear-gradient(#141414, #333333);
         color:#fafafa;
         font-size:1em;
         font-weight:bold;
         border:1px solid #333333;
     }
      .Button:hover
     {
         padding:7px 20px 7px 20px;
         background:linear-gradient(#333333, #141414);
         color:#ff0000;
         border:1px solid #333333;
     }
    </style>

C# Code : Login Button Click


 private void Btn_Logic_Click(object sender, EventArgs e)
  {
   BusinessAccessLayer.BusinessLogic bl = new BusinessAccessLayer.BusinessLogic();
   bool result;
   result = bl.IsValidUser(textBox1.Text, textBox2.Text);
   if (result == true)
   MessageBox.Show("Successfully Logged in!");
 else
   MessageBox.Show("Invalid Login");
  }

DataAccessLayer :


  1. Add Class File and rename class1.cs into Databaselogic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
 namespace DataAccessLayer
  {
   public class DatabaseLogic
    {
     public DataTable GetUsers(string Username, string Password)
      {
        //connection logic
       string cnstr = ConfigurationSettings.AppSettings["RemoteDatabase"];
       SqlConnection cn = new SqlConnection(cnstr);
       //stmt logic
       string sqlstr = "select * from users where username= ' " + Username + " ' and password= ' " + Password + " ' ";
       //adapter logic
       SqlDataAdapter adp = new SqlDataAdapter(sqlstr, cn);
       DataSet ds = new DataSet();
       adp.Fill(ds);
       //datatable logic
       DataTable dt;
       dt = ds.Tables[0];
       return (dt);
      }
    }
  } 

BusinessAccessLayer :


  1. Add Class file and rename class1.cs into BusinessLogic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
 namespace BusinessAccessLayer
  {
   public class BusinessLogic
   {
    public bool IsValidUser(string Username, string Password)
     {
      DataTable dt;
      DataAccessLayer.DatabaseLogic dbl = new DataAccessLayer.DatabaseLogic();
      dt = dbl.GetUsers(Username, Password);
      if (dt.Rows.Count > 0)
      return true;
    else
      return false;
     }
   }
 }