Create a Login Form in ASP.NET using SQL Server Database and Visual Studio 2022

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


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Login Form</title>

    <style>

        body {

            text-align:center;

            margin-top:10rem;

            font-size:2rem;

        }

        form {

            border:solid;

            margin-left:30%;

            margin-right:30%;

            padding-bottom:10px;

        }

        div {

            padding:5px;

        }

        .btn-login {

            margin-left:10%;

        }

        .checkbox {

            margin-left:15%;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:Label ID="Label1" runat="server" Text="Login Form" ForeColor="#FF0066"></asp:Label>

        </div>

        <div>

            <asp:Label ID="Label2" runat="server" Text="Username"></asp:Label>

            <asp:TextBox ID="TextBox1" runat="server" Height="35px"></asp:TextBox>

        </div>

        <div>

            <asp:Label ID="Label3" runat="server" Text="Password"></asp:Label>

            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Height="35px"></asp:TextBox>

        </div>

        <input class="checkbox" type="checkbox" onchange="document.getElementById('TextBox2').type=this.checked? 'text': 'password'"/>Show Password

        <div>

            <asp:Button CssClass="btn-login" ID="Button1" runat="server" Text="Login" BackColor="#33CC33" ForeColor="White" Height="30px" Width="80px" OnClick="Button1_Click" Font-Size="Medium"/>

            <asp:Button ID="Button2" runat="server" Text="Cancel" BackColor="Red" ForeColor="White" Height="30px" Width="80px" Font-Size="Medium"/>

        </div>

    </form>

</body>

</html>

Above File is LoginFormWebApp\login.aspx








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;

namespace LoginFormWebApp
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=HEER\\SQLEXPRESS;Initial Catalog=asplogin;Integrated Security=True;TrustServerCertificate=True");
            con.Open();
            string loginQuery = "SELECT COUNT(*) FROM login WHERE username=@username AND password=@password";
            SqlCommand cmd = new SqlCommand(loginQuery, con);
            cmd.Parameters.AddWithValue("@username",TextBox1.Text);
            cmd.Parameters.AddWithValue("@password",TextBox2.Text);
            int count = (int)cmd.ExecuteScalar();
            con.Close();
            if (count > 0)
            {
                Response.Write("<script>alert('login success');</script>");
            }
            else
            {
                Response.Write("<script>alert('login error')</script>");
            }
        }
    }
}
Above File is LoginFormWebApp\login.aspx.cs














Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET