Sunday, October 11, 2015

VB.NET Login System

This is a VB.NET Login System that I created in a WT Delivery Monitoring System.

The Login System will prompt you for a Username and Password and check if that Username and Password exists on MySQL database.

Once the Username and Password are validated it will display the Main Program.


Figure 1. WTCI Delivery Monitoring System Login


Figure 2. Login System


Once you entered the correct Username and Password on the Login System it will lead you to the Main Program of the Delivery Monitoring System or Any Form that has meaningful functions.

Figure 3. Empty Login

If you enter a blank Username and Password as shown in "Figure 3. Empty Login" it will display a Messagebox to "Enter Correct Username and Password to Login"

If you don't have an account yet a LinkLabel named "Create Account" will display the "User Account" form and ask for your credentials to be save on MySQL database as shown in "Figure 4. User Account Maintenance"

Figure 4. User Account Maintenance.


The figure above displays the "User Account Maintenance" in which it displays the information of the user in DatagridView format the idlogin, Username, Full Name and Role.

You can also create new user account once you are in this login page, delete user, edit user, find user, and print information from the database.

Figure 5. Welcome Screen

As shown in the figure above the Welcome message is displayed once the correct Username and Password are entered correctly into the Login Form and this will lead you to open the Main Form of the Program - in this case the WTCI Delivery Login Form.

Once you press the OK button this will open the Main Form as shown in the figure below.

Figure 6. Main Program


From Figure 2. Login System on the OK button write this piece of code to validate the Users' correct Username and Password.

This is my code:
--------------------
Private Sub OK_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles OK.Click
        Try
            Dim conn As MySqlConnection
            conn = New MySqlConnection
 
            conn.ConnectionString = "Data Source= localhost; Database = wtci; User Id = root; Password = root"
 
            If UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Then
                MsgBox("Please Enter Correct 'Username' and 'Password' to Login"MsgBoxStyle.Information)
 
            Else
 
                conn.Open()
 
                Dim myadapter As New MySqlDataAdapter
                Dim sqlquery As String
                'sqlquery = String.Format("SELECT * FROM login where Username = '" + UsernameTextBox.Text + "' And '" + PasswordTextBox.Text + "'")
                sqlquery = String.Format("SELECT * FROM login WHERE username = '{0}' AND password = '{1}'"Me.UsernameTextBox.Text.Trim(), Me.PasswordTextBox.Text.Trim())
 
 
                Dim mycommand As New MySqlCommand
                mycommand.Connection = conn
                mycommand.CommandText = sqlquery
 
                'Start query
                myadapter.SelectCommand = mycommand
                Dim mydata As MySqlDataReader
                mydata = mycommand.ExecuteReader
 
 
                'See if the user exists
                If mydata.HasRows = 0 Then
                    MsgBox("Invalid Username and password?"MsgBoxStyle.Critical)
 
                Else
                    Dim idlogin As Boolean
                    While mydata.Read()
                        idlogin = mydata.GetInt32("idlogin")
                    End While
 
                    MsgBox("Welcome" + " " + UsernameTextBox.Text + "!")
 
                    If idlogin = True Then
                        DeliveryMonitoringForm.Show()
                    End If
                    Me.Hide()
                End If
            End If
 
            'DeliveryMonitoringForm.Show()
        Catch ex As Exception
            MsgBox(ex.ToString)
 
        End Try
 
    End Sub
 
    Private Sub Cancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Cancel.Click
        Me.Close()
    End Sub
-------------------------------------------------------------------------------------------

Before I forgot I am using MySQL database server please add this line of code at the topmost portion of your code:

 - Imports MySQL.Data.MySQLClient

Before adding "Imports MySQL.Data.MySQLClient" to your code, you have to locate the MySQL.Data.dll from .NET Connector as a Reference.

login is the table with username and password fields


Yes No Cancel Button

This is a VB.NET code excerpt from the program I develop from WT Construction - Delivery Monitoring System. This piece of small code will let you select Yes No Cancel button once you are going to exit on the program.

If you select Yes upon exit, it will exit from the program else it will remain on the program.

Figure 1. MenuStrip

As you can see on Figure 1 the exit button is selected.


Figure 2. Yes No Cancel MessageBox

After the Exit button is selected the Yes No Cancel  MessageBox prompt displays.

If you press Yes it will literally exit from the program, else it will remain on the program.

This is my code:
-------------------
 Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgsHandles ExitToolStripMenuItem.Click
        Dim exit_result As Integer = MsgBox("Are you sure you want to exit?"MsgBoxStyle.YesNoCancel)
 
        If exit_result = vbYes Then
            Me.Close()
        End If
 
        
 
    End Sub