Login User and User Registration Form in Visual Basic 2008 and MySQL Database

In this tutorial I will show you how to create a Login User and User Registration Form using Visual Basic 2008 and MySQL Database. In this features, you can Register and Login what you’ve Registered in the User Registration Form. Login Form is very Important in making a System because this serves as a Protection in your System from Invaders. It will also filter the Users on what functionalities they are going to use. So lets’ begin: First, create a table in the MySQL Database named “userdb”.
  1. CREATE TABLE IF NOT EXISTS `users` (
  2. `name` varchar(30) NOT NULL,
  3. `username` varchar(30) NOT NULL,
  4. `Pass` varchar(60) NOT NULL,
  5. `type` varchar(30) NOT NULL,
  6. PRIMARY KEY (`id`)
After that, create a Registration and Login in a Form, and it will look like this. Registration Form Then double click the Form and set up the connection of MySQL Database and Visual Basic 2008 in global.
  1. Imports MySql.Data.MySqlClient
  2. Public Class Form1
  3.  
  4. 'create a public function and set your MySQL Connection .
  5. Public Function mysqlconnection() As MySqlConnection
  6. 'return new connection.
  7. Return New MySqlConnection("server=localhost;user id=root;database=userdb")
  8. End Function
  9.  
  10. 'put the value of mysqlconnection() as MySQL connection to con.
  11. Public con As MySqlConnection = mysqlconnection()
  12.  
  13. End Class
Declare all the Classes that you needed.
  1. 'declaring the classes
  2. 'a set of command in MySQL
  3. Dim cmd As New MySqlCommand
  4.  
  5. 'represent a new spicific table in the database
  6. Public dt As New DataTable
  7.  
  8. 'serve as the bridge of mysql database and the datatable for saving and retrieving data.
  9. Public da As New MySqlDataAdapter
Create a Sub Procedure for Registration.
  1. 'create a sub procedure named "create" and the parameter's type is string
  2. Public Sub create(ByVal sql As String)
  3.  
  4. Try
  5. 'open the connection
  6. con.Open()
  7. 'set your command
  8. With cmd
  9. 'holds the data to be executed
  10. .Connection = con
  11. .CommandText = sql
  12. 'execute the data
  13. result = cmd.ExecuteNonQuery
  14.  
  15. 'coditioning the result whether succesfull or failed when it is executed.
  16. If result = 0 Then
  17. 'the execution of data is failed
  18. MsgBox("Registration failed!")
  19. Else
  20. 'the executed data is succesfull
  21. MsgBox("You are now registered.")
  22. End If
  23. End With
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26. End Try
  27. con.Close()
  28. End Sub
Create a Sub Procedure for login.
  1.  
  2. 'create a sub procedure named "loginUser" and the parameter's type is string
  3. Public Sub loginUser(ByVal sql As String)
  4. Try
  5.  
  6.  
  7.  
  8. 'declaring the variable as integer
  9. Dim maxrow As Integer
  10.  
  11. 'open the connection
  12. con.Open()
  13.  
  14. 'it will set a new specific table in the database
  15. dt = New DataTable
  16.  
  17. 'set a command
  18. With cmd
  19. 'holds the data
  20. .Connection = con
  21. .CommandText = sql
  22. End With
  23.  
  24. 'retriving the data
  25. da.SelectCommand = cmd
  26. da.Fill(dt)
  27.  
  28. 'put the max value of rows in the table to a variable(maxrow)
  29. maxrow = dt.Rows.Count
  30.  
  31. 'conditioning the total value of rows in the table
  32. 'if the total value of rows in the table is greater than 0 then the result is true
  33. ' and if not, the result is false
  34. If maxrow > 0 Then
  35. 'appearing the record of the current row and the current column in the current table.
  36. MsgBox(dt.Rows(0).Item(1) & " , " & dt.Rows(0).Item(4))
  37. Else
  38. 'the result is false
  39. MsgBox("Account does not exist.")
  40.  
  41. End If
  42. Catch ex As Exception
  43. MsgBox(ex.Message)
  44. End Try
  45. con.Close()
  46. da.Dispose()
  47. End Sub
Go back to Design Views, double click the Save Button and do this.
  1. Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
  2.  
  3. 'put the string value to sql
  4. sql = "INSERT INTO `users` (`name`, `username`, `Pass`, `type` ) VALUES ('" _
  5. & txtname.Text & "','" & txtuser.Text & "','" & txtpass.Text & "','" & cbotype.Text & "')"
  6.  
  7. 'call your sub name and put the sql in the parameters list.
  8. create(sql)
  9.  
  10. End Sub
Go back to Design Views, double click the Login Button and do this.
  1. Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
  2.  
  3. 'put the string value to sql
  4. sql = "SELECT * from users WHERE username = '" & username.Text & "' and Pass = '" & password.Text & "'"
  5.  
  6. 'call your sub name and put the sql in the parameters list.
  7. loginUser(sql)
  8.  
  9. End Sub
Run your project. The complete Source Code is included. Download and run it on your computer.

Comments

Add new comment