Create Multi User Login Form in ASP.NET using SQL Server Database

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


<!DOCTYPE html>


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

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>

            <table align="center">

                <tr>

                    <td>Username</td>

                    <td>

                        <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>Password</td>

                    <td>

                        <asp:TextBox ID="txtPass" runat="server"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>Select User Type</td>

                    <td>

                        <asp:DropDownList ID="DropDownList1" runat="server">

                            <asp:ListItem Value="admin"></asp:ListItem>

                            <asp:ListItem Value="user"></asp:ListItem>

                        </asp:DropDownList>

                    </td>

                </tr>

                <tr>

                    <td></td>

                    <td>

                        <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click"/>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

ABOVE FILE IS WebForm1.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;
using System.Data;
using System.Configuration;
using System.Threading;

namespace MultiUserLoginForm
{
    public partial class home : 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)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    string query = "SELECT * FROM aspmuser WHERE username=@username AND password=@password AND usertype=@usertype";
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.Parameters.AddWithValue("@username", txtUser.Text);
                    cmd.Parameters.AddWithValue("@password", txtPass.Text);
                    cmd.Parameters.AddWithValue("@usertype", DropDownList1.SelectedItem.ToString());
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable data = new DataTable();
                    sda.Fill(data);
                    if (data.Rows.Count > 0)
                    {
                        Response.Write("<script>alert('you are logged in as " + data.Rows[0][2] + "')</script>");
                        if (DropDownList1.SelectedIndex == 0)
                        {
                            //Server.Transfer("admin.aspx");
                            Response.Redirect("admin.aspx");
                            //Server.Execute("admin.aspx");
                        }
                        else if (DropDownList1.SelectedIndex == 1)
                        {
                            //Server.Transfer("user.aspx");
                            Response.Redirect("user.aspx");
                            //Server.Execute("user.aspx");
                        }
                    }
                    else
                    {
                        Response.Write("Error in your input");
                    }
                }
            }
            catch (SqlException ex)
            {
                Response.Write("SqlException: " + ex.Message);
            }
            catch (Exception ex)
            {
                Response.Write("Exception: " + ex.Message);
            }
        }
    }
}
ABOVE FILE IS WebForm1.aspx.cs














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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Admin Page</h1>
        </div>
    </form>
</body>
</html>
ABOVE FILE IS admin.aspx













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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>User Page</h1>
        </div>
    </form>
</body>
</html>
ABOVE FILE IS user.aspx














<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=DESKTOP-77M6N4G\SQLEXPRESS;Initial Catalog=aspmuser;Integrated Security=True;TrustServerCertificate=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
ABOVE FILE IS Web.config














CREATE DATABASE aspmuser;

USE aspmuser;

CREATE TABLE aspmuser(
username NVARCHAR(50) NOT NULL,
password NVARCHAR(50) NOT NULL,
usertype NVARCHAR(50) NOT NULL
);

INSERT INTO aspmuser(username,password,usertype)
VALUES('admin','admin','admin'),
('admin','admin','user');

SELECT * FROM aspmuser;






Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022

SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar