Login User and User Registration Form in VB.net
In this tutorial, I will show you how to create a User Registration and Login Form in VB.net and MySQL Database. Login form vb code will be discussed thoroughly in this lesson.
With this, you can log in the user that you have registered in the Registration Form. Using the login form vb code, you can create your visual studio login form database.
What is Visual Basic’s purpose in Login User and User Registration Form in VB.net?
The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.
However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.
What are the most important characteristics of Visual Basic?
Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.
But if you want to start how to code using visual studio 2019, you can start your lesson here on how to connect Mysql to visual Studio 2019.
You can use this login code in visual basic 2010 if you are planning to create a visual basic login form using access database.
To start with this project, follow the given steps:
- Create first a MySQL database and name it as ” dbuser “.[Login User and User Registration Form in VB.net]
- Then we will add a new table named “users”. (Execute the query below.)
CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(40) NOT NULL, `user_username` varchar(40) NOT NULL, `user_pass` varchar(90) NOT NULL, `user_type` varchar(20) NOT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB;
3. After creating the table in MySQL Database, open the Visual Basic.
4. Create a new Windows Form Application. Set up the Form just like this.[Login User and User Registration Form in VB.net]
5. Go to the solution explorer and hit the view code.(Login User and User Registration Form in VB.Net)
6. 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=dbuser") '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
7. 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
8. 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
9. Go back to the Form Design, double click the ”Save” button.
10. Set a query in saving 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 users ( `user_name`, `user_username`, `user_pass`, `user_type`)" & _ "VALUES ('" & txtname.Text & "','" & txtusername.Text & "','" & txtpassword.Text & "','" & cbotype.Text & "')" register(sqlQuery) End Sub
11. Go back to the Form Design again, double click the ”Login” button.
12. 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 user_username ='" & txtuname.Text & "' AND user_pass = '" & txtpass.Text & "'" login(sqlQuery) End Sub
13. Press “F5” to run the program.
The complete Source Code of the login vb.net MySQL can be downloaded here.