How to Create a Login form in Visual Basic.Net and MySQL Database

Submitted by: 
Visitors have accessed this post 99873 times.

In this tutorial, I'm going to show you how to create a simple Login-Logout system using Visual basic.net and MySQl Database. To start with this application, open Visual Basic->Create a New Project->Save it as “Login”.
This time, let’s add objects to our windows form and these objects are the following: four Labels, two Textbox,two buttons and a Groupbox.

Designing object Properties

Object Property Settings

Label1 Name lbllogin
Text Login
Label2 Name lblname
Text Hi,Guest!
Label3 Text Username
Label4 Text Password
Textbox1 Name txtuname
Textbox2 Name txtpass
PasswordChar *
Button1 Name btnok
Text OK
Button2 Name btncancel
Text Cancel

After setting the Object properties, arrange all the objects like as shown below.

This time, let’s we are now ready to add functionality to our application. To do this, double click the form and add the following code:
This code will simply disable the group box that holding the label Username and Password same with the two textbox and two Buttons.

  1. GroupBox1.Enabled = False

Next, double click the “lbllogin” label and add the following code:
This code will check if the “lbllogin” is set to “Logout” then it reset the text of “lbllogin” to “Login” same with the “lblname” to “Hi, Guest!”, else if the text of “lbllogin” is equal to “Login” then it's enabled the Group box.

  1. If lbllogin.Text = "Logout" Then
  2. lbllogin.Text = "Login"
  3. lblname.Text = "Hi, Guest!"
  4. ElseIf lbllogin.Text = "Login" Then
  5. GroupBox1.Enabled = True
  6.  
  7. End If

Then add the following code under the public class.

  1. 'Represents an SQL statement or stored procedure to execute against a data source.
  2. Dim cmd As New MySqlCommand
  3. Dim da As New MySqlDataAdapter
  4. 'declare conn as connection and it will now a new connection because
  5. 'it is equal to Getconnection Function
  6. Dim con As MySqlConnection = jokenconn()
  7.  
  8. Public Function jokenconn() As MySqlConnection
  9. Return New MySqlConnection("server=localhost;user id=root;password=;database=studentdb")
  10. End Function

And double click the “OK” button and add the following code:

  1. Dim sql As String
  2. Dim publictable As New DataTable
  3. Try
  4. 'check if the textbox is equal to nothing then it will display the message below!.
  5. If txtuname.Text = "" And txtpass.Text = "" Then
  6. MsgBox("Password or Username Incorrect!")
  7.  
  8. Else
  9. sql = "select * from tbluseraccounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
  10. 'bind the connection and query
  11. With cmd
  12. .Connection = con
  13. .CommandText = sql
  14. End With
  15. da.SelectCommand = cmd
  16. da.Fill(publictable)
  17. 'check if theres a result by getting the count number of rows
  18. If publictable.Rows.Count > 0 Then
  19.  
  20. 'it gets the data from specific column and assign to the variable
  21. Dim user_type, name As String
  22. user_type = publictable.Rows(0).Item(4)
  23. name = publictable.Rows(0).Item(2)
  24. 'check if the type of user is admin
  25. If user_type = "Admin" Then
  26. 'welcomes the user as Admiistrator
  27. MsgBox("Welcome " & name & " you login as Administrator ")
  28. 'set the lbllogin text to Logout
  29. lbllogin.Text = "Logout"
  30. 'disabled the groupbox
  31. GroupBox1.Enabled = False
  32. 'reset all the two textbox
  33. txtuname.Text = ""
  34. txtpass.Text = ""
  35. 'set the lblname to the specific user
  36. lblname.Text = "Hi, " & name
  37.  
  38. ElseIf user_type = "Encoder" Then
  39. MsgBox("Welcome " & name & " you login as Encoder ")
  40. lbllogin.Text = "Logout"
  41. GroupBox1.Enabled = False
  42. txtuname.Text = ""
  43. txtpass.Text = ""
  44. lblname.Text = "Hi, " & name
  45. Else
  46. MsgBox("You login as Guest!")
  47. lbllogin.Text = "Logout"
  48. GroupBox1.Enabled = False
  49. txtuname.Text = ""
  50. txtpass.Text = ""
  51. lblname.Text = "Hi, " & name
  52. End If
  53.  
  54. Else
  55. MsgBox("Contact administrator to registered!")
  56. txtuname.Text = ""
  57. txtpass.Text = ""
  58. End If
  59.  
  60. da.Dispose()
  61. End If
  62.  
  63. Catch ex As Exception
  64. MsgBox(ex.Message)
  65.  
  66. End Try
  67. con.Clone()

This time you can now test your application by pressing “F5”.

And here's the database table used.

  1. CREATE TABLE IF NOT EXISTS `tbluseraccounts` (
  2. `userID` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(255) DEFAULT NULL,
  4. `users_name` varchar(255) DEFAULT NULL,
  5. `userpassword` varchar(255) DEFAULT NULL,
  6. `usertype` varchar(255) DEFAULT NULL,
  7. PRIMARY KEY (`userID`)
  8.  
  9. --
  10. -- Dumping data for table `tbluseraccounts`
  11. --
  12.  
  13. INSERT INTO `tbluseraccounts` (`userID`, `username`, `users_name`, `userpassword`, `usertype`) VALUES
  14. (1, 'admin', 'Joken Villanueva', 'admin123', 'Admin'),
  15. (2, 'jason', 'Jason Batuto', 'jason123', 'Encoder');

After testing the program, here’s all the following code used in this application.

  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class Form1
  4. 'Represents an SQL statement or stored procedure to execute against a data source.
  5. Dim cmd As New MySqlCommand
  6. Dim da As New MySqlDataAdapter
  7. 'declare conn as connection and it will now a new connection because
  8. 'it is equal to Getconnection Function
  9. Dim con As MySqlConnection = jokenconn()
  10.  
  11. Public Function jokenconn() As MySqlConnection
  12. Return New MySqlConnection("server=localhost;user id=root;password=;database=studentdb")
  13. End Function
  14. Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
  15. Dim sql As String
  16. Dim publictable As New DataTable
  17. Try
  18. 'check if the textbox is equal to nothing then it will display the message below!.
  19. If txtuname.Text = "" And txtpass.Text = "" Then
  20. MsgBox("Password or Username Incorrect!")
  21.  
  22. Else
  23. sql = "select * from tbluseraccounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
  24. 'bind the connection and query
  25. With cmd
  26. .Connection = con
  27. .CommandText = sql
  28. End With
  29. da.SelectCommand = cmd
  30. da.Fill(publictable)
  31. 'check if theres a result by getting the count number of rows
  32. If publictable.Rows.Count > 0 Then
  33.  
  34. 'it gets the data from specific column and assign to the variable
  35. Dim user_type, name As String
  36. user_type = publictable.Rows(0).Item(4)
  37. name = publictable.Rows(0).Item(2)
  38. 'check if the type of user is admin
  39. If user_type = "Admin" Then
  40. 'welcomes the user as Admiistrator
  41. MsgBox("Welcome " & name & " you login as Administrator ")
  42. 'set the lbllogin text to Logout
  43. lbllogin.Text = "Logout"
  44. 'disabled the groupbox
  45. GroupBox1.Enabled = False
  46. 'reset all the two textbox
  47. txtuname.Text = ""
  48. txtpass.Text = ""
  49. 'set the lblname to the specific user
  50. lblname.Text = "Hi, " & name
  51.  
  52. ElseIf user_type = "Encoder" Then
  53. MsgBox("Welcome " & name & " you login as Encoder ")
  54. lbllogin.Text = "Logout"
  55. GroupBox1.Enabled = False
  56. txtuname.Text = ""
  57. txtpass.Text = ""
  58. lblname.Text = "Hi, " & name
  59. Else
  60. MsgBox("You login as Guest!")
  61. lbllogin.Text = "Logout"
  62. GroupBox1.Enabled = False
  63. txtuname.Text = ""
  64. txtpass.Text = ""
  65. lblname.Text = "Hi, " & name
  66. End If
  67.  
  68. Else
  69. MsgBox("Contact administrator to registered!")
  70. txtuname.Text = ""
  71. txtpass.Text = ""
  72. End If
  73.  
  74. da.Dispose()
  75. End If
  76.  
  77. Catch ex As Exception
  78. MsgBox(ex.Message)
  79.  
  80. End Try
  81. con.Clone()
  82.  
  83.  
  84. End Sub
  85.  
  86. Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click
  87.  
  88. If lbllogin.Text = "Logout" Then
  89. lbllogin.Text = "Login"
  90. lblname.Text = "Hi, Guest!"
  91. ElseIf lbllogin.Text = "Login" Then
  92. GroupBox1.Enabled = True
  93.  
  94. End If
  95. End Sub
  96.  
  97. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  98. GroupBox1.Enabled = False
  99. End Sub
  100. End Class

Comments

need help to subject

I'm thinking line 5
If txtuname.Text = "" And txtpass.Text = "" Then
should Be
If txtuname.Text = "" Or txtpass.Text = "" Then
That way it would match your comment and properly test for null strings.

hello sir,can u send me an example of supply inventory system how to design and log in form .

I'm thinking line 5
If txtuname.Text = "" And txtpass.Text = "" Then
should Be
If txtuname.Text = "" Or txtpass.Text = "" Then
That way it would match your comment and properly test for null strings.
 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.