VB.NET MySQL: Create Login Form

Hi there.....

This article is about how to create login form using VB.NET and database MySQL. For the connection I use MySQL Connector that can be download here:

And how to use/add it into your project please read this article:

We'll make a simple login without encryption. If you need a sample reference for encryption, you can have it here: https://rani-irsan.blogspot.com/2021/09/vbnet-encryption-with-bytes-and-md5.html

I've already created a project, which I use on the serial of VB.NET MySQL tutorial. It's already has a form named Form 1.


Table user that will be use is look like this:



We'll add a login form using template. Go to Solution explorer, right click on project node -> Add -> Windows form.




Choose Login Form then click Add.



This design UI automatically generated.



Some codes also generated.



Remove code that we don't use. Since we'll use MySQL we need to import MySql.Data.MySqlClient.
Imports MySql.Data.MySqlClient

Declare variable named iFail for counting failure of login. Because application will be terminated after 3 failure login.

Public Class LoginForm1
 

    Dim iFail As Integer

End Class

Then give an initial value  = 0 to this iFail variable in the form_load event.
Private Sub LoginForm1_Load(ByVal sender As Object, _
     ByVal As System.EventArgs) Handles Me.Load
     iFail = 0
End Sub


For checking whether login values are correct, let's make a function named CheckLogin that will return an integer value. It will return 1 if correct otherwise is 0.

Function CheckLogin(ByVal UserName As String, _
    ByVal Password As StringAs Integer

    
Dim conn As MySqlConnection
    
Dim cmd As New MySqlCommand
    
Dim objValue As Object

    conn = 
New MySqlConnection()
    conn.ConnectionString = "server=localhost;user id=root;" & _
                                "password=;database=latihan"
    Try
        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = "SELECT COUNT(username) AS getin " & _
                          "FROM tbluser WHERE username = " & _
                          "'" & UserName & "' AND " & _
                          "password = '" & Password & "'"

        objValue = cmd.ExecuteScalar()
        conn.Close()
        If objValue Is Nothing Then
            Return 0
        Else
            Return
 CInt(objValue)
        End If
    Catch
 myerror As MySqlException
        MessageBox.Show("Error: " & myerror.Message)
        Return 0
    Finally
        conn.Dispose()
    End Try
    Return 
0
End Function

 
Login process is started by filling username and password, then click OK button. Below is how to write code under OK_Click event. 

Private Sub OK_Click(ByVal sender As System.Object, _
        ByVal As System.EventArgs) Handles OK.Click
    Dim strUserName As String = UsernameTextBox.Text
    
Dim strPassword As String = PasswordTextBox.Text

    If strUserName = String.Empty Then _
       MsgBox("Username is empty!") : Exit Sub
    
If strPassword = String.Empty Then _
       MsgBox("Password is empty!") : Exit Sub

    Try
        If
 CheckLogin(strUserName, strPassword) > 0 Then
            MsgBox("Welcome " & strUserName & "!")
            'write other needed code here if login is success
            Me.Close()
        Else
            iFail = iFail + 1
            If iFail >= 3 Then
                MsgBox("Failed 3 times." & vbCrLf & _
                       "Application will be closed!")

                End
            End If
            MsgBox("Username/password incorrect." & vbCrLf & _
                   "Please check again!")
        End If
    Catch
 ex As Exception
        MsgBox("Error Login: " & ex.Message)
    End Try
End Sub


Last is the Cancel button. This button is used to close the login form if we want to cancel login process. 

Private Sub Cancel_Click(ByVal sender As System.Object, _
        ByVal As System.EventArgs) Handles Cancel.Click

    Me.Close()

End Sub



See also video about this on youtube:



Post a Comment

1 Comments

The Skill Pedia said…
This comment has been removed by the author.