In this VB tutorial, Let’s create a login and registration form in Visual Basic VB.NET with the MYSQL database. In this task, the user can run the application, register a new user, and log in with the same user name and password and the data will be stored in the MySQL database using the user table.
Registration form is a list of input fields to get the user information and a submit button to save the data into the database. There are many reasons that you would want a person to fill out a registration form. Many companies use registration forms to sign up customers for subscriptions, services, or other programs.
Create Login and Registeration form in VB with MYSQL
To start this project, follow the given steps:
Step 1:
- Create first a MySQL database in your workbench and name it as ” userdata “
//Create database Create database userdata;
- Then we will add a new table named “user_info”.
// Create table CREATE TABLE `user_info` ( `id` INT NOT NULL AUTO_INCREMENT , `names` VARCHAR(50) NOT NULL , `username` VARCHAR(20) NOT NULL , `password` VARCHAR(50) NOT NULL , PRIMARY KEY (`id`)) ;
Step 2:
- After creating the table in MySQL Database, then we have to open the Visual Basic.
- Create a new Windows Form Application. Set up the Form1 just like this.
Step 3:
- In next step go to solution explorer and click on view code.
- In the code view, set a vb.net MySQL Connection string and declare all the classes and variables that are needed.
'SET THE CONNECTION BETWEEN VISUAL BASIC AND MYSQL DATABASE Dim con As MySqlConnection = New MySqlConnection("server=localhost; user id=root; password=; database=userdata") 'A SET OF COMMAND IN MYSQL Dim cmd As New MySqlCommand 'SET A CLASS THAT SERVES AS THE BRIDGE BETWEEN A DATASET AND DATABASE FOR SAVING AND RETRIEVING DATA. Dim da As New MySqlDataAdapter 'SET A CLASS THAT CONSISTS SPECIFIC TABLE IN THE DATABASE Dim dt As New DataTable Dim sqlQuery As String Dim result As Integer
Step 4:
Now we will create a Sub Procedure for the registration of the user.
Private Sub register(ByVal sqlQuery As String) Try 'OPENING THE CONNECTION con.Open() 'HOLDS THE DATA TO BE EXECUTED With cmd .Connection = con .CommandText = sqlQuery End With 'EXECUTE THE DATA result = cmd.ExecuteNonQuery 'CHECKING IF THE DATA HAS BEEN EXECUTED OR NOT If result > 0 Then MsgBox("User has been registered.") Else MsgBox("Failed to register the user.") End If con.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Step 5:
After creating the Sub Procedure for the registration of the user, we will now create another Sub Procedure for the login of the user.
Private Sub login(ByVal sqlQuery As String) Try con.Open() With cmd .Connection = con .CommandText = sqlQuery End With 'FILLING THE DATA IN A SPECIFIC TABLE OF THE DATABASE da.SelectCommand = cmd dt = New DataTable da.Fill(dt) 'DECLARING AN INTEGER TO SET THE MAXROWS OF THE TABLE Dim maxrow As Integer = dt.Rows.Count 'CHECKING IF THE DATA IS EXIST IN THE ROW OF THE TABLE If maxrow > 0 Then MsgBox("Welcome " & dt.Rows(0).Item(4)) Else MsgBox("Account does not exist.") End If Catch ex As Exception MsgBox(ex.Message) End Try con.Close() End Sub
Step 6:
- Now go back on the Form Designing and double click on the ”Save” button.
- Write a query for saving the data in the database and call a method that you have created for the registration of the user.
Private Sub btnreg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreg.Click sqlQuery = "INSERT INTO user_info (`name`,`username,`password`)" & _ "VALUES ('" & txtname.Text & "','" & txtusername.Text & "','" & txtpassword.Text & "','"')" register(sqlQuery) End Sub
Step 7:
- Then go back to the Form Design, double click the ”Login” button.
- Set the query for retrieving data in the database and call the method that you have created for the Login of the user.
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click sqlQuery = "SELECT * FROM users WHERE username ='" & txtusername.Text & "' AND password = '" & txtpassword.Text & "'" login(sqlQuery) End Sub
Step 8:
Now press “F5” and run the program. first, you will see the form1 on which you will see two sections in the first half the registration fields and the registration button and the other half is login fields and the login button. Fill the required fields for what you want to do either register or login and then click the related button. You will see that finally, you have registered or login in to the application.
Output:
So in this way you can create the login and registration application in VB.NET with MySQL.