ARTICLE

Simple User Login in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 09, 2011
Here, we will create in this article. 1. Registration Form 2. Login Form 3. Email existence Form
Download Files:
 
Reader Level:

Introduction

In this article we will create a registration form, login form and checkemail existing form. in SQL database we create table and stored procedure.

1. Registration Form

Step-1

Create Table

Now creating a table in SQL server database which has the username, password and email fields. Table looks like this.

create table registrationtab

(

Username varchar(100),

Email varchar(100),

Password varchar(20)

)

Step-2

Create stored procedure

Now Creating stored procedure for registration. That means values will be insert in table using stored procedure. Stored procedure looks like that for it.

create procedure [dbo].[storlogin134]

(

@username varchar(40),

@email varchar(50),

@password varchar(20)

)

as

insert into registrationtab  values(@username,@email,@password )

Step-3

Create form for registration

Now creating a form in asp.net with the following field which are defined in the table. The form looks like below figure.

change11.gif

Figure1

Now double click on the register me button and add following code.

Protected Sub Buttonregisterme_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Buttonregisterme.Click

        Dim strcon As String = "Data Source=.;uid=sa;pwd=Password$2;database=master"

        Dim con As New SqlConnection(strcon)

        Dim com As New SqlCommand("storlogin134", con)

        com.CommandType = CommandType.StoredProcedure

        Dim p1 As New SqlParameter("username", TextBoxusername.Text)

        Dim p2 As New SqlParameter("email", TextBoxemail.Text)

        Dim p3 As New SqlParameter("password", TextBoxpassword.Text)

        com.Parameters.Add(p1)

        com.Parameters.Add(p2)

        com.Parameters.Add(p3)

        con.Open()

        com.ExecuteNonQuery()

        Labelinfo.Text = "registered successful."

    End Sub

 

Now run the application and enter the username, email and password and then click on the register me Button to save the values in database.


change12.gif

 

Figure2

 

Open the database and check it in registrationtab table.

2. Login form

Step-4

Create stored procedure

Now Creating stored procedure for login. That means values will be match in table using stored procedure. Stored procedure looks like that for it.

create PROCEDURE CheckUser

(

@username as varchar(50),

@password as varchar(50)

)

AS

SELECT * FROM registrationtab WHERE username=@username AND password=@password

Now creating a form in asp.net with the following field which are defined in the table. The form looks like below figure.

login4.gif

Figure3

Step-5

Create form for login

Now creating a form in asp.net with the username and password field which are defined in the table. The form looks like below figure.

Figure4

Now double click on the login button and add following code.

Protected Sub Buttonlogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Buttonlogin.Click

        Dim strcon As String = "Data Source=.;uid=sa;pwd=Password$2;database=master"

        Dim con As New SqlConnection(strcon)

        Dim com As New SqlCommand("CheckUser", con)

        com.CommandType = CommandType.StoredProcedure

        Dim p1 As New SqlParameter("username", TextBoxusername.Text)

        Dim p2 As New SqlParameter("password", TextBoxpassword.Text)

        com.Parameters.Add(p1)

        com.Parameters.Add(p2)

        con.Open()

        Dim rd As SqlDataReader = com.ExecuteReader()

        If rd.HasRows Then

            rd.Read()

            Labelinfo.Text = "Login successful."

        Else

            Labelinfo.Text = "Invalid username or password."

        End If

    End Sub


Now run the application and enter the username and password and then click on the login Button to match the values with database. Suppose we enter wrong username and password.


login3.gif

 

Figure4

 

Now enter correct username and password.


login5.gif

 

Figure5

 

3. check existence

 

Step-6

 

Check email Existence

Now Creating stored procedure for Check email existence. Stored procedure looks like that for it.

create PROCEDURE existance

(

 

@email as varchar(50)

)

AS

SELECT * FROM registrationtab WHERE email= @email

Now creating a form in asp.net with the email field which are defined in the table. The form looks like below figure.

check13.gif

Figure6

Step-7

Now double click on the login button and add following code.

Protected Sub Buttonchekexistance_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Buttonchekexistance.Click

        Dim strcon As String = "Data Source=.;uid=sa;pwd=Password$2;database=master"

        Dim con As New SqlConnection(strcon)

        Dim com As New SqlCommand("existance", con)

        com.CommandType = CommandType.StoredProcedure

        Dim p1 As New SqlParameter("email", TextBoxemail.Text)

        com.Parameters.Add(p1)

        con.Open()

        Dim rd As SqlDataReader = com.ExecuteReader()

        If rd.HasRows Then

            rd.Read()

            Me.Labelinfo.ForeColor = System.Drawing.Color.Red

            Me.Labelinfo.Text = "already Exist!"

        Else

            Me.Labelinfo.Text = "you can login now"

            Me.TextBoxusername.Text = ""

            Me.TextBoxpassword.Text = ""

            Me.TextBoxemail.Text = ""

        End If

    End Sub

 

Now run the application and enter the username, email and password to check existence emailid with database. Suppose we enter a new email id.


check14.gif

 

Figure7

 

Now and then click on the check existence Button.


check15.gif

 

Figure8

 

Now enter a exist email id and click on the check existence Button.


check16.gif

 

Figure9

 

Login to add your contents and source code to this article
share this article :
post comment
 

Rohatash, Really i have searched many time in google that type of article but can't get it. it's a really helpful article.

Posted by Kamal Verma Jul 13, 2011
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor