• Signup
  • Publish with Us
  • Hosting Hub
  • Newsroom
  • Explore Us
  • Login
  • In Twitter
  • Blog Posting Services
  • In LinkedIn
  • Our Code Repository
J
HARAPHULA
OneStop Shop of Information

Updates

  • The Role of Gaming in Education: How Games are used to Teach and Engage
    •
  • Mobile Repairing Course in Rawalpindi – Learn to Fix iPhones and Android Phones
    •
  • The Role of Pricing Procedures in SAP SD and their Configuration
    •
  • Top-rated best Commerce Coaching in Patna for 11 and 12
    •
  • BCA Distance Education in India – Best Universities, Fees and Career Guide
    •
  • Why Pursuing a Cisco Certification Course Can Transform your IT Career?
    •
  • The 9 Things you must know to Prepare for IIT JEE
    •
  • CMA US – Your Pathway to a Rewarding Career in Management Accounting
    •
  • Top 8 Advantages of Taking the IIT JAM Exam
    •
  • How LMS Tools Empower Collaborative Learning Experiences?
    •
  • Explore the Leading Computer Teacher Training Diploma Courses in 2025
    •
  • How to Choose the best ADCA Institute for your Career Goals?
    •
  • Is CCA the Right Course for Non-IT Background Students?
    •
  • Your Guide to MBBS in Russia – Eligibility, Fees, and Admission
    •
  • Understanding the Core Concepts of ADCA
    •

ASP.NET Login form Example validating user from SQL Server Database

Microsoft Technologies
May 15, 2016
4 (1 votes)
ASP.NET Login form Example validating user from SQL Server Database ASP.NET Login form Example validating user from SQL Server Database
2 5 80
Making you Earn. Developing Web2 Blogs with CPM Ads

Designing a Login form is not so easy as we are thinking. Modern login pages comes with various rich feature such as Validating User, Form Validation, Capcha or Remember Password. In this Login form Example demo app we are going to show you how to authenticate an user using records from SQL Server user table. To start with first you required to Create a user table in SQL Server.

Create a SQL Table for Users

To Create an user table which will store Login details such as UserID or Password your required to run the following Transact-SQL Query.

CREATE TABLE user_table (
id INT PRIMARY KEY, 
userid VARCHAR(255) NOT NULL, 
password VARCHAR(56)
);

Then insert some sample records.

In front-end Copy n Paste my default.aspx page Codes. Here I have 2 textbox Controls such as user id and password. To stop spam here I am using required field validation for both the fields. Additionally to valid an email id I am using ASP.NET RegularExpressionValidator validation control. Then in my form I have a submit button. Which submits the form to server using post method.

ASP.NET Login form Example

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>ASP.NET Login form Example</title>
<link rel="stylesheet" href="master.css" />
</head>
<body>
<form id="frmLogin" runat="server">
<div class="loginDiv rounded">
<div class="formControl">
Email ID&nbsp;<span style="color: Red;">*</span>
</div>
<div class="formControl">
<asp:TextBox ID="txtEmailID" runat="server" Width="100%"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmailID" runat="server" ControlToValidate="txtEmailID"
Display="Dynamic" ErrorMessage="Email should not be blank.<br />"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmailID" runat="server"
ControlToValidate="txtEmailID" Display="Dynamic" ErrorMessage="Enter a valid Email ID.<br />"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</div>
<div class="formControl">
Password&nbsp;<span style="color: Red;">*</span>
</div>
<div class="formControl">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="100%"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
Display="Dynamic" ErrorMessage="Password should not be blank."></asp:RequiredFieldValidator>
</div>
<asp:Button ID="btnVisitorLogin" runat="server" Text="Login" />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<input type="checkbox" value="remember-me" />&nbsp;Remember me<a href="#" class="pull-right margin-gap">Need help?</a>
</div>
</form> 
</body>
</html>

To valid user from SQL Server in Code behind file I am executing my business logics inside “btnVisitorLogin_Click” event. To connect and execute SQL query which will fetch data I imported the system files “System.Data” and “System.Data.SqlClient” in page header.

To establish a Connection I am creating a new instance for SQL Server Connection object. Which accepts connection string from my web.config file. Look at the below code to know how to declare connection string in web.config file.

<connectionStrings>
<add name="connStr" connectionString="Data Source=WINSERV;Initial Catalog=phase2;User ID=sa;Password=win#serv" providerName="System.Data.SqlClient"/>
</connectionStrings>

Here Data Source is my Database Server name. Initial Catalog is the Database name and User id, password is for the database user.

To compare user Credentials with database records here I am executing a command object and fetching user table records to a SQL DataReader. As you know datareader works like a pointer. Using a DataReader compare to a DataSet we can minimize the login time. To validate a user I am running a while loop until the end record of SQL DataReader “dr_pass”. For each record inside the while loop I am comparing the value with user password. In case it returns true I am allowing the user to redirect into the application dashboard page.

Default.aspx.vb

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnVisitorLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVisitorLogin.Click

Dim oMyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ToString)
Dim oMyCommand As New SqlCommand("SELECT password, userid FROM user_table WHERE userid= '" & txtEmailID.Text & "' ", oMyConnection)
Dim dr_pass As SqlDataReader

oMyConnection.Open()
dr_pass = oMyCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

If Not dr_pass.HasRows Then
lblMessage.Text = "Invalid Email or Password."
End If

While dr_pass.Read
If Trim(txtPassword.Text) = dr_pass("password") Then
Session.Add("sess_uid", dr_pass("userid"))
Response.Redirect("../Dashboard.aspx")
End If
End While
End Sub
End Class

Login Form CSS Style (master.css)

#frmLogin { max-width: 300px; margin: 0 auto; padding-top: 120px; }
.formControl { font-family: Verdana; font-size: 12px; }
.loginDiv { width:300px; margin-top: 20px; padding: 20px 20px 20px 20px; background-color: #f7f7f7; -moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); -webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); } 
.rounded { border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; }

The above demo will run without CSS. But beauty attracts eyes. This is the cause I am with the above CSS classes. Apply this CSS to have a basic look. You modify it as per the mock-up.

Tags:ASP.NET Login form, ASP.NET Login Page, Database Basics, SQL Server Database, validating user from
Email Marketing plan for Small/Medium Size Businesses
Introduction to TCP/IP Model Layers for absolute Beginners
Responsive mobile first BootStrap 5 User Login page Example
FREE PHP Widgets to boost Conversion and low Bounce Rates
demo

Related Posts

  • Microsoft ASP.NET Interview Questions with Answers
    Microsoft ASP.NET Interview Questions with Answers
  • ASP.NET Login page example with Remember me Option
    ASP.NET Login page example with Remember me Option
  • List of frequently used common SQL Queries with Example
    List of frequently used common SQL Queries with Example
  • Web Storage (LocalStorage) vs Web SQL vs IndexedDB in HTML5
    Using LocalStorage Objects vs Web SQL Database in HTML5
  • ADO.NET Architecture with Examples for beginners
    ADO.NET ExecuteNonQuery, ExecuteReader, ExecuteScalar Examples
  • How to implement Paging & Sorting in ASP.NET Gridview Example?
    How to implement Paging and Sorting in ASP.NET Gridview Example?
  • How to update records in a Gridview using Auto Generate Edit Button?
    How to update records in a Gridview using Auto Generate Edit Button?
  • Example of Resume for IT professionals to get their Dream Job
    Example of Resume for IT professionals to get their Dream Job
  • How to design a Database? - Database design for Beginners
    How to Design a Database? - Database Design for Beginners
  • PHP Treeview Example using data from MySQL Database
    PHP Treeview Example using data from MySQL Database
  • How to display icon images in Gridview or Datagrid rows?
    How to display various Files icon images in Datagrid rows?
  • Database Basics with Terminologies definition for Beginners
    Database Basics with Terminologies definition for Beginners
Bloggers with AdSense Ads Go for the Top rated CPM Ad Network to Earn better

OUR FACILITIES

  • Signup
  • Login
  • Our Background
  • Policies
  • WordPress.org

CONTACT INFO

  • Reach Us
  • WhatsApp +919096266548

PUBLISH WITH US

Small Business Owners try the power of Article Marketing to grab Leads from Google or Local Search. Organic viewers are the real buyers. Double your Sales with our high DA lifelong SEO backlinks.

WHY ONESTOP?

We are here to develop high Quality Information. As a Multi-niche platform, We worked Several years for Collecting various useful Stories. Dream to establish a Domain where you can get all your day-to-day required information. We Covers Animals to Zoology.
©2014-2026 JHARAPHULA, ALL RIGHTS RESERVED.