Thursday, 15 April 2021

how to create a login page in asp.net using ado.net SQL database

  Overview:

This article explains how to create a login page in asp.net using the ado.net  SQL database. then username and password do not exist in the database table and show an alert message.  The following steps here.

Download Zip file

Step 1:  Create SQL table as the name is tblLogin


USE [Demo]

GO


/****** Object:  Table [dbo].[tblLogin]    Script Date: 15-04-2021 21:00:30 ******/

SET ANSI_NULLS ON

GO


SET QUOTED_IDENTIFIER ON

GO


CREATE TABLE [dbo].[tblLogin](

[ID] [int] IDENTITY(1,1) NOT NULL,

[Username] [nvarchar](50) NULL,

[Password] [nvarchar](50) NULL,

 CONSTRAINT [PK_tblLogin] PRIMARY KEY CLUSTERED 

(

[ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO


Step 2:  Create form Design


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table class="auto-style1">
                <tr>
                    <td colspan="2" style="color: #0000FF; font-size: x-large">Create Login page in asp.net c# using ado.net</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login" />
                    </td>
                </tr>
            </table>

        </div>
           </form>
</body>
</html>
        
Output : Form design
login form in asp.net c#

login form asp.net


Step 3:  Add database connection in the web config file.

  <connectionStrings>
    <add name="constring" connectionString="Data Source=LAPTOP-VOAQ0VQC;Initial Catalog=Demo;Integrated Security=True"/>
  </connectionStrings>

Step 4: Write  Csharp code in the login button click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DemoInsert
{
    public partial class Login : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {

                string sqlquery = "select 1 from tblLogin where Username='" + txtusername.Text + "' and Password='" + txtpassword.Text + "'";
                SqlCommand cmd = new SqlCommand(sqlquery);
                cmd.Connection = con;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr.Read())
                {
                    Response.Write("<script>alert('Login Success')</script>");
                }
                else
                {
                    Response.Write("<script>alert('Username and password are not exists')</script>");
                }
                con.Close();
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

Step 5: After Running View

No comments:

Post a Comment

Angular Course Class Day-4 in English

  Angular Course Class Day-4 Note: You have to add following code your project and run check the result. Angular Material UI Design: CSS...