Imports MySql.Data.MySqlClient
Public Class Form1
Const AppName = "MySQL Login System"
Private Sub cmdAuthenticate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAuthenticate.Click
If txtUserName.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("Please enter the required information to login.", AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Dim mMySQLConnectionString As String = "server=localhost; user id=myroot; password=mypwd; database=login"
'You will need to change the connection string above to yours
Dim conn As MySqlConnection
Dim dr As MySqlDataReader
Dim cmd As New MySqlCommand
conn = New MySqlConnection()
conn.ConnectionString = mMySQLConnectionString
Try
conn.Open() 'Always a good idea to open the connection in a try/catch block
'Before you were selecting the username and password..
'I changed that to the Status field being that its the field we need.
cmd.CommandText = "SELECT Status FROM members WHERE UserName = ?UserName AND Password = ?Password"
cmd.Parameters.Add(New MySqlParameter("?UserName", txtUserName.Text))
cmd.Parameters.Add(New MySqlParameter("?Password", txtPassword.Text))
cmd.Connection = conn
dr = cmd.ExecuteReader
If dr.HasRows() Then
'I used a datareader to hold the result of the Select Statement
'which will be the status field if the username AND password are CORRECT
'If the UserName and Password are wrong, the datareader will not hold any rows.
dr.Read()
Select Case UCase(dr(0).ToString)
Case Is = "ADMIN"
Dim F2 As New Form2
F2.Show()
Me.Close()
Case Is = "MOD"
Dim F3 As New Form3
F3.Show()
Me.Close()
Case Is = "USER"
Dim F3 As New Form3
F3.Show()
Me.Close()
End Select
Else
MsgBox("Either the User Name or Password are Incorrect. Please try again.", MsgBoxStyle.Information, AppName)
txtPassword.ResetText()
txtUserName.Focus()
End If
Catch myerror As MySqlException
MessageBox.Show("Database Error: " & myerror.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
'The Finally Section of a Try Catch block will always fire, even when it runs into an exception.
'That makes it a good place to clean up
cmd.Parameters.Clear() 'I always clear my parameters when im done with them just to be safe
conn.Close()
End Try
End If
End Sub
End Class