im making a mysql sigin form and im trying to use my user table to control log in access but whenever i type in any random letters it logs into without showing the error that its an invalid username.
This is what i have so far:
Imports MySql.Data.MySqlClient
Public Class frmLogin
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
Application.Exit()
End Sub
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myConnString As String
Dim UserID As String
myConnString = "server=" & My.Settings.HostIP & ";" _
& "user id=" & My.Settings.Username & ";" _
& "password=" & My.Settings.Password & ";" _
& "database=attendance"
conn.ConnectionString = myConnString
Try
conn.Open()
myCommand.Connection = conn
myCommand.CommandText = "SELECT user_bannerid FROM user WHERE BINARY username = ?Username"
myCommand.Parameters.Add("?Username", txtUsername.Text)
UserID = myCommand.ExecuteScalar
conn.Close()
Dim mainForm As New frmMain
mainForm.UserID = UserID
mainForm.connectionString = myConnString
mainForm.Show()
Me.Hide()
Me.Close()
Catch myerror As MySqlException
MessageBox.Show("Invalid User. Please Enter Your Correct Username")
conn.Dispose()
End Try
End Sub
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AcceptButton = cmdLogin
Me.CancelButton = cmdCancel
End Sub
End Class
This is because you are not testing the value of userid before closing the form.
You also need to implement using statements to ensure that all disposable items are properly disposed.
Here is a rewrite that solves both issues: