0% found this document useful (0 votes)
20 views

PR Reviewer

This document contains code for a login form that connects to a MySQL database. It defines functions for connecting to the database and validating login credentials against database tables. When the login button is clicked, it checks if the username and password fields are empty. If not empty, it queries the database to check if the credentials match and grants access to the main form if so, or denies access if not matched. It also imports MySQL libraries and defines a connection object and command object to connect to and query the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

PR Reviewer

This document contains code for a login form that connects to a MySQL database. It defines functions for connecting to the database and validating login credentials against database tables. When the login button is clicked, it checks if the username and password fields are empty. If not empty, it queries the database to check if the credentials match and grants access to the main form if so, or denies access if not matched. It also imports MySQL libraries and defines a connection object and command object to connect to and query the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports MySql.Data.

MySqlClient

Public Class frmLogin


Dim cp As Integer

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click


Me.Close()
End Sub

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

If txtUsername.Text = "" Or txtPassword.Text = "" Then 'Restriction 1: All field


must have values.
MsgBox("Please supply required fields.", MsgBoxStyle.Exclamation, "Message
Prompt")

txtPassword.Text = ""
txtUsername.Select()
Else 'Restriction 2: Data must matched to ur database.

If con.State = ConnectionState.Closed Then


con.Open()
End If

cmd = New MySqlCommand("Select * FROM tblLogin WHERE UName = @UName and PWord
= @PWord", con) 'cmd = new mysqlcommand ("query",database location/path)
cmd.Parameters.AddWithValue("@UName", txtUsername.Text)
cmd.Parameters.AddWithValue("@PWord", txtPassword.Text)

dr = cmd.ExecuteReader
dr.Read()

If dr.HasRows Then 'If dta matched to our database

MsgBox("ACCESS GRANTED: Hi! " & txtUsername.Text & ", welcome to Student
Management System Dashboard.", MsgBoxStyle.Information, "LOGIN SECURITY: Access Granted")

txtUsername.Text = ""
txtPassword.Text = ""

Me.Hide()

frmMain.Show()

Else 'If not matched."

MsgBox("ACCESS DENIED: Either username or password is incorrect",


MsgBoxStyle.Exclamation, "LOGIN SECURITY: Access Denied.")

txtPassword.Text = ""
txtUsername.Select()

End If
End If

dr = Nothing
con.Close()
'If txtUsername.Text = "" Or txtPassword.Text = "" Then

' MsgBox("Please supply required fields.", MsgBoxStyle.Exclamation,


"Message Prompt")

' txtPassword.Text = ""


' txtUsername.Select()

'Else

' If con.State = ConnectionState.Closed Then


' con.Open()
' End If

' cmd = New MySqlCommand("Select * FROM tblLogin WHERE UName = @UName and
PWord = @PWord", con)

' cmd.Parameters.AddWithValue("@UName", txtUsername.Text)


' cmd.Parameters.AddWithValue("@PWord", txtPassword.Text)

' dr = cmd.ExecuteReader
' dr.Read()

' If dr.HasRows Then


' MsgBox("ACCESS GRANTED: Hi! " & txtUsername.Text & ", welcome to
Student Management System Dashboard.", MsgBoxStyle.Information, "LOGIN SECURITY: Access
Granted")

' txtUsername.Text = ""


' txtPassword.Text = ""

' Me.Hide()

' frmMain.Show()

' Else
' MsgBox("ACCESS DENIED: Either username or password is incorrect",
MsgBoxStyle.Exclamation, "LOGIN SECURITY: Access Denied.")

' txtPassword.Text = ""


' txtUsername.Select()

' cp += 1

' If cp = 3 Then

' MsgBox("Student Management System application will be


automatically terminated.", MsgBoxStyle.Critical, "Termination Notification")

' End

' End If

' End If

' dr = Nothing
' con.Close()
' End If

End Sub

Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

DBConnect()

End Sub
End Class

'To import MySQL Libraries


Imports MySql.Data
Imports MySql.Data.MySqlClient

Module modConnection
Public con As New MySqlConnection 'Function used to set the connectionstring.
Public cmd As New MySqlCommand 'Function used to set the SQL.
Public dr As MySqlDataReader

Public Sub DBConnect()


Try

With con

.ConnectionString = "server=localhost; user id= root;


password= ;database= smsdatabase" 'To connect to our database. Serves path location for
our database.
.Open() 'We connect to our database.

'To display or notify the user that we are connected to our database.
'MsgBox("This application is now connected to the database.",
vbInformation, "SMSDB: Message Prompt")
End With

Catch ex As Exception

'MsgBox(ex.Message.ToString) 'To check the error.

'If something error happen....


MsgBox("Unable to connect. Please contact the system administrator!",
vbExclamation)
con.Close()

End Try
End Sub

End Module

You might also like