How to Create Login and Register Form in Visual Basic.Net with MySQL Database
In This VB.NET Tutorial We Will See How To Make a SignIn and a SignUp Form With Design and Connection With MySQL Database To Allow The Users To Create Their Account or To Login Into The Application Using Visual Basic.Net Windows Form and Visual Studio Editor .
What We Are Gonna Use In This Project:
- Visual Basic.Net Programming Language.- Visual Studio Editor.
- xampp server.
- MySQL Database.
- PhpMyAdmin.
- Canva.com( to create the images )
What We Will Do In This Project:
- Design Two Forms For The Login and Register Using Panels, TextBoxes, Buttons and Labels.- Create a Custom Buttons To Close The Application .
- Create a Class For The Connection With MySQL Database.
- Connect VB.Net To MySQL Database.
- Allow The Users To Create Their Accounts On Register Form.
- Allow The Users To Access The Application On Login Form
- Check If The User Leave Some Fields Empty.
- Check if The Username Already Exists.
- Check If The User Enter a Wrong Password In The Confirmation Field.
WATCH THIS VB.Net TUTORIAL
Project Source Code:
' Create a Class To Connect Our Application With The MySQL Database
Imports MySql.Data.MySqlClient
Public Class MY_CONNECTION
' first we need to download the mysql connector & add it to our project
' https://dev.mysql.com/downloads/connector/net/8.0.html
' open xampp
' create the mysql database using phpmyadmin
' create the database and name it "vbnet_users_db"
' create a table and name it "users"
' create the connection
Private connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=vbnet_users_db")
' return the connection
ReadOnly Property getConnection() As MySqlConnection
Get
Return connection
End Get
End Property
' open the connection
Sub openConnection()
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
End Sub
' close the connection
Sub closeConnection()
If connection.State = ConnectionState.Open Then
connection.Close()
End If
End Sub
End Class
' Create The Register Form To Allow Users To Create Their Account
Imports MySql.Data.MySqlClient
Public Class RegisterForm
Private Sub RegisterForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' transparent black background for panel2
Panel2.BackColor = Color.FromArgb(120, 0, 0, 0)
' transparent blue background for panel3
Panel3.BackColor = Color.FromArgb(100, 17, 97, 238)
ButtonClose.BackColor = Color.FromArgb(100, 0, 0, 0)
End Sub
' button close click
Private Sub ButtonClose_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Application.Exit()
End Sub
' textbox first name enter
Private Sub TextBoxFirstname_Enter(sender As Object, e As EventArgs) Handles TextBoxFirstname.Enter
LabelFN.ForeColor = Color.Red
End Sub
' textbox first name leave
Private Sub TextBoxFirstname_Leave(sender As Object, e As EventArgs) Handles TextBoxFirstname.Leave
LabelFN.ForeColor = Color.White
End Sub
' textbox last name enter
Private Sub TextBoxLastname_Enter(sender As Object, e As EventArgs) Handles TextBoxLastname.Enter
LabelLN.ForeColor = Color.Red
End Sub
' textbox last name leave
Private Sub TextBoxLastname_Leave(sender As Object, e As EventArgs) Handles TextBoxLastname.Leave
LabelLN.ForeColor = Color.White
End Sub
' textbox username enter
Private Sub TextBoxUsername_Enter(sender As Object, e As EventArgs) Handles TextBoxUsername.Enter
LabelUN.ForeColor = Color.Red
End Sub
' textbox username leave
Private Sub TextBoxUsername_Leave(sender As Object, e As EventArgs) Handles TextBoxUsername.Leave
LabelUN.ForeColor = Color.White
End Sub
' textbox email enter
Private Sub TextBoxEmail_Enter(sender As Object, e As EventArgs) Handles TextBoxEmail.Enter
LabelEM.ForeColor = Color.Red
End Sub
' textbox email leave
Private Sub TextBoxEmail_Leave(sender As Object, e As EventArgs) Handles TextBoxEmail.Leave
LabelEM.ForeColor = Color.White
End Sub
' textbox password enter
Private Sub TextBoxPassword_Enter(sender As Object, e As EventArgs) Handles TextBoxPassword.Enter
LabelPASS1.ForeColor = Color.Red
End Sub
' textbox password leave
Private Sub TextBoxPassword_Leave(sender As Object, e As EventArgs) Handles TextBoxPassword.Leave
LabelPASS1.ForeColor = Color.White
End Sub
' textbox confirm password enter
Private Sub TextBoxConfirmPassword_Enter(sender As Object, e As EventArgs) Handles TextBoxConfirmPassword.Enter
LabelPASS2.ForeColor = Color.Red
End Sub
' textbox confirm password leave
Private Sub TextBoxConfirmPassword_Leave(sender As Object, e As EventArgs) Handles TextBoxConfirmPassword.Leave
LabelPASS2.ForeColor = Color.White
End Sub
' button register click
Private Sub ButtonRegister_Click(sender As Object, e As EventArgs) Handles ButtonRegister.Click
' check if the fields are empty
' check if the password = the confirm password
' check if the username already exists
' get textboxes values
Dim fname As String = TextBoxFirstname.Text
Dim lname As String = TextBoxLastname.Text
Dim username As String = TextBoxUsername.Text
Dim email As String = TextBoxEmail.Text
Dim password As String = TextBoxPassword.Text
Dim cpassword As String = TextBoxConfirmPassword.Text
If fname.Trim() = "" Or lname.Trim() = "" Or username.Trim() = "" Or email.Trim() = "" Or password.Trim() = "" Then
MessageBox.Show("One Or More Fields Are Empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf Not String.Equals(password, cpassword) Then
MessageBox.Show("Wrong Confirmation Password", "password Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf usernameExist(username) Then
MessageBox.Show("This Username Already Exists, Choose Another One", "Duplicate Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
' add the new user
Dim conn As New MY_CONNECTION()
Dim command As New MySqlCommand("INSERT INTO `users`(`first_name`, `last_name`, `email`, `username`, `password`) VALUES (@fn, @ln, @mail, @usn, @pass)", conn.getConnection)
command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = fname
command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = lname
command.Parameters.Add("@mail", MySqlDbType.VarChar).Value = email
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = username
command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = password
conn.openConnection()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Registration Completed Successfully", "User Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.closeConnection()
Else
MessageBox.Show("Something Happen", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
conn.closeConnection()
End If
End If
End Sub
' create a function to check if the username already exists
Public Function usernameExist(ByVal username As String) As Boolean
Dim con As New MY_CONNECTION()
Dim table As New DataTable()
Dim adapter As New MySqlDataAdapter()
Dim command As New MySqlCommand("SELECT * FROM `users` WHERE `username` = @usn", con.getConnection())
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = username
adapter.SelectCommand = command
adapter.Fill(table)
' if the username exist return true
If table.Rows.Count > 0 Then
Return True
' if not return false
Else
Return False
End If
End Function
' button close mouse enter
Private Sub ButtonClose_MouseEnter(sender As Object, e As EventArgs) Handles ButtonClose.MouseEnter
ButtonClose.ForeColor = Color.White
End Sub
' button close mouse leave
Private Sub ButtonClose_MouseLeave(sender As Object, e As EventArgs) Handles ButtonClose.MouseLeave
ButtonClose.ForeColor = Color.Black
End Sub
' label go to signin mouse enter
Private Sub LabelGoToSignIn_MouseEnter(sender As Object, e As EventArgs) Handles LabelGoToSignIn.MouseEnter
LabelGoToSignIn.ForeColor = Color.White
End Sub
' label go to signin mouse leave
Private Sub LabelGoToSignIn_MouseLeave(sender As Object, e As EventArgs) Handles LabelGoToSignIn.MouseLeave
LabelGoToSignIn.ForeColor = Color.Black
End Sub
' label go to signin mouse click
Private Sub LabelGoToSignIn_Click(sender As Object, e As EventArgs) Handles LabelGoToSignIn.Click
Me.Hide()
Dim lForm As New Login_Form()
lForm.Show()
End Sub
End Class
' Create The Login Form To Allow The User To Access The Application
Imports MySql.Data.MySqlClient
Public Class Login_Form
Private Sub Login_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' transparent black background for panel2
Panel2.BackColor = Color.FromArgb(120, 0, 0, 0)
' transparent blue background for panel3
Panel3.BackColor = Color.FromArgb(100, 17, 97, 238)
ButtonClose.BackColor = Color.FromArgb(100, 0, 0, 0)
End Sub
' button close mouse enter
Private Sub ButtonClose_MouseEnter(sender As Object, e As EventArgs) Handles ButtonClose.MouseEnter
ButtonClose.ForeColor = Color.White
End Sub
' button close mouse leave
Private Sub ButtonClose_MouseLeave(sender As Object, e As EventArgs) Handles ButtonClose.MouseLeave
ButtonClose.ForeColor = Color.LightGray
End Sub
' button close click
Private Sub ButtonClose_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Application.Exit()
End Sub
' textbox username enter
Private Sub TextBoxUsername_Enter(sender As Object, e As EventArgs) Handles TextBoxUsername.Enter
' clear the textbox when the textbox get the focus
Dim username As String = TextBoxUsername.Text
' check if the username is empty
' check if the textbox contains the default value "username"
If username.Trim().ToLower() = "username" Or username.Trim() = "" Then
' clear the textbox
TextBoxUsername.Text = ""
' change the textbox for color
TextBoxUsername.ForeColor = Color.Black
End If
End Sub
' textbox username leave
Private Sub TextBoxUsername_Leave(sender As Object, e As EventArgs) Handles TextBoxUsername.Leave
' when the textbox get lost the focus
Dim username As String = TextBoxUsername.Text
' check if the username is empty
' check if the textbox contains the default value "username"
If username.Trim().ToLower() = "username" Or username.Trim() = "" Then
' set the textbox text
TextBoxUsername.Text = "username"
' change the textbox for color
TextBoxUsername.ForeColor = Color.DarkGray
End If
End Sub
' textbox password enter
Private Sub TextBoxPassword_Enter(sender As Object, e As EventArgs) Handles TextBoxPassword.Enter
' when textbox password has focus
Dim pass As String = TextBoxPassword.Text
If pass.Trim().ToLower() = "password" Or pass.Trim() = "" Then
' clear the textbox text
TextBoxPassword.Text = ""
' change the textbox font color
TextBoxPassword.ForeColor = Color.Black
' use system password
TextBoxPassword.UseSystemPasswordChar = True
End If
End Sub
' textbox password leave
Private Sub TextBoxPassword_Leave(sender As Object, e As EventArgs) Handles TextBoxPassword.Leave
' when textbox password lost focus
Dim pass As String = TextBoxPassword.Text
If pass.Trim().ToLower() = "password" Or pass.Trim() = "" Then
' set the textbox text
TextBoxPassword.Text = "password"
' change the textbox font color
TextBoxPassword.ForeColor = Color.DarkGray
' set system password to false
TextBoxPassword.UseSystemPasswordChar = False
End If
End Sub
' button login click
Private Sub ButtonLogin_Click(sender As Object, e As EventArgs) Handles ButtonLogin.Click
' before the user can login we need to check if the textboxes are empty
' if they contains the default values( username & password )
' check if this user exist in the database
Dim conn As New MY_CONNECTION()
Dim adapter As New MySqlDataAdapter()
Dim table As New DataTable()
Dim command As New MySqlCommand("SELECT `username`, `password` FROM `users` WHERE `username` = @usn AND `password` = @pass", conn.getConnection())
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = TextBoxUsername.Text
command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = TextBoxPassword.Text
If TextBoxUsername.Text.Trim() = "" Or TextBoxUsername.Text.Trim().ToLower() = "username" Then
MessageBox.Show("Enter Your Username To Login", "Missing Username", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf TextBoxPassword.Text.Trim() = "" Or TextBoxPassword.Text.Trim().ToLower() = "password" Then
MessageBox.Show("Enter Your Password To Login", "Missing Password", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
adapter.SelectCommand = command
adapter.Fill(table)
If table.Rows.Count > 0 Then
Me.Hide()
Dim mainAppForm As New ApplicationMainForm()
mainAppForm.Show()
Else
MessageBox.Show("This Username Or/And Password Doesn't Exists", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
' label go to signup form mouse enter
Private Sub LabelGoToSignUp_MouseEnter(sender As Object, e As EventArgs) Handles LabelGoToSignUp.MouseEnter
LabelGoToSignUp.ForeColor = Color.White
End Sub
' label go to signup form mouse leave
Private Sub LabelGoToSignUp_MouseLeave(sender As Object, e As EventArgs) Handles LabelGoToSignUp.MouseLeave
LabelGoToSignUp.ForeColor = Color.Black
End Sub
' label go to signup form click
Private Sub LabelGoToSignUp_Click(sender As Object, e As EventArgs) Handles LabelGoToSignUp.Click
Me.Hide()
Dim rForm As New RegisterForm()
rForm.Show()
End Sub
End Class
OUTPUT:
Register Form |
Register Form - TextBox Enter |
Register Form - Error |
Login Form |
Login Form - Error |
Main Application Form |
if you want the source code click on the download button below
More VB.NET Projects:
- VB.NET Inventory System Source Code- VB.Net Contact Information Management System Source Code
- VB.Net Hotel System Source Code
Download Projects Source Code
6 comments
commentsMySql.Data.MySqlClient.MySqlException: 'Unknown column 'first_name' in 'field list''
ReplyHelp me, in register used for error compilation
ReplyThank You for Posting such a great Post. cloud security management
ReplyExcellent Piece of Information. Thanks server migration to cloud
ReplyGood and Informative Blog! aws backup service
Replyi would not use this code it is not secure even if u get it working first mysql injection will work in this code it is not secure use at own risk never store database connection in vb.net app u can get raw text for the connection after compiled Private connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=vbnet_users_db")
Reply