Login and Logout in Visual basic VB.NET Windows Application

Create a windows application in VB.NET that will take user input on the login page which is connected with our database login table, and check the input that they are matching or not if yes it will take us to the main page of the application which is Homepage and on the Home page there we will create one logout button by clicking on that it will redirect us to the login page again. So now we know that we have to create,

  • One SQL database table.
  • And two forms in our project of Visual Basic windows application.

Login and logout in VB.NET Windows Application

Let’s start with the following steps,

Step 1:

First we will create one SQL database in MySQL workbench > Open your MySQL workbench > create new database with any name you want I gave my database name as My_Database > and then we have to make one table in it with the name login_tbl here we will take two columns which are Username and Password give their data type as varchar (20) and varchar (10) respectively,


click on apply for the script > and fill some data in the table in both the columns > and Save it.

Step 2:

Now we will create a windows application for it, Open your visual studio > go to File menu > click on New project

> in visual Basic select Windows Form Application > give your project name I have given its name as Login Application > click on OK.

If you want to change your Form1 name then change it I have given my Form1 name as loginform. On login form take two labelstwo Text boxes, and one button on it.

Give the label’s name as Username and Password and the button name as Login.

Step 3:

Now we will add one more form to our application and name it as a Homepage which will be displayed when we will fill correct username and password on the login page which will match our database table. I have  taken one label and one button on it.

and give the label text “This is Homepage” and one button on it and named it Logout because I am here showing the logout code and working on it, if you want to design more things on the Homepage you can do that.

Step 4:

Now we have completed all the designing part of our application so let’s move to the coding part of it. Here we have to Import MySQL.Data.MySqlClient namespace.

Imports MySql.Data.MySqlClient
Public Class Login_Form
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=root;database=My_Database")

Here also we have to double click on the login button which will create a button click event write the below code on login form.vb.

Imports MySql.Data.MySqlClient 
Public Class Login_Form 
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=root;database=My_Database")

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonLogin.Click

Dim command As New MySql command("SELECT `username`,password` FROM ,`login_tbl` WHERE username` = @username AND `password` = @password", connection) command.Parameters.Add("@username", MySqlDbType.VarChar) .Value = TextBox1.Text command.Parameters.Add("@password", MySqlDbType.VarChar) .Value = TextBox2.Text Dim adapter As New MySqlDataAdapter(command) Dim table As New DataTable() adapter.Fill(table) If table.Rows.Count = 0 Then MessageBox.Show("Invalid Username Or Password") Else MessageBox.Show("Login Successful!") Homepage.Show() Me.Hide() End If End Sub End Class

Step 5:

Now go to Homepage.vb file and click on the logout button to create a click event, add the below code there. By clicking on the logout button the event will close the main form and redirect us to login form.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
loginform.Show()

Step 6:

Now run the application.

Output:

first the login page will display to us then if we do not enter any data into Text boxes.if we entered some data but if it’s not matching with our database table which is connected with our application so then it will show the warning “Invalid Username Or Password”.

When we fill in the correct username and password and if it is present in our data table then by clicking on the login button.

In the above example, I have entered the data it already exists in my connected login_tbl which I have made before. That will show the message on the screen in the message box that is “Login Successful!”. By clicking on the OK button of that message box, It will take us to the Homepage.

And there when we click on the logout button it will again take us back to the login page and stop showing the Homepage.

So in this way, we can simply make the application which does the login and logout operation in VB.NET.