How to create Login page using database Connection in ASP.NET -Example


Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="18_Login.aspx.cs" Inherits="_18_Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
            height: 82px;
        }
        .auto-style2 {
            text-align: right;
        }
        .auto-style3 {
            width: 21px;
        }
        .auto-style4 {
            width: 100%;
        }
        .auto-style5 {
            width: 268px;
        }
        #form1 {
            height: 278px;
            text-align: center;
        }
        .auto-style6 {
            text-align: left;
        }
    </style>
</head>
<body style="height: 78px">
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
                </td>
                <td class="auto-style3">:-</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBox1" runat="server" Height="25px" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                </td>
                <td class="auto-style3">:-</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBox2" runat="server" Height="25px" TextMode="Password"
Width="150px"></asp:TextBox>
                </td>
            </tr>
        </table>
   
    </div>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Username OR passsword is incorrect Try again."
Visible="False"></asp:Label>
        <br />
        <br />
        <table align="right" class="auto-style4">
            <tr>
                <td class="auto-style5" style="text-align: right">
                    <asp:Button ID="Button1" runat="server" Height="30px" OnClick="Button1_Click" Text="CANCEL"
Width="100px" />
                </td>
                <td style="text-align: left">
                    <asp:Button ID="Button2" runat="server" Height="30px" OnClick="Button2_Click" Text="LOGIN"
Width="100px" />
                </td>                                                                                                          
            </tr>
        </table>
        <br />
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" Text="Hi...Welcome...!" Visible="False"></asp:Label>
    </form>
</body>
</html>


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 System.Data.SqlClient;
using System.Data;

public partial class _18_Login : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Asp_dot_ne\WebSite3\App_Data\pro_gridview.mdf;Integrated Security=True");
    SqlDataAdapter adp;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox2.Text == "")
        {
            Label3.Visible = true;
            Label4.Visible = false;
        }
        else
        {
            conn.Open();
            adp = new SqlDataAdapter("select * from Login where usnm='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "'", conn);
            dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                Label4.Visible = true;
                Label3.Visible = false;
            }
            else
            {
                Label3.Visible = true;
                Label4.Visible = false;
            }
            conn.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
}

Share this

Related Posts

Previous
Next Post »