公共类frmAdminLoginpage
Dim ErrorCount As Integer
Private Sub frmAdminLoginpage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ErrorCount = 0
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
Me.Close()
End Sub
Private Sub lblCreateAccount_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lblCreateAccount.LinkClicked
frmRegister.Show()
Me.Hide()
End Sub
Private Sub lblForgotPassword_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lblForgotPassword.LinkClicked
frmForgotPassword.Show()
Me.Hide()
End Sub
Sub ClearControls()
txtLoginID.Text = ""
txtLoginPassword.Text = ""
txtLoginID.Focus()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear text
ClearControls()
End Sub
Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim strConnectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim strSQL As String
' Check if ID or password is empty
If txtLoginPassword.Text = "" Or txtLoginID.Text = "" Then
MessageBox.Show("Please Enter your ID and Password.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' Both fields was supply
' Check if user exist in database
' Connect to Database
strConnectionString = "Data Source=LENOVO-PC; Initial Catalog=VB; Integrated Security=True"
Try
'Database records will verify the Staff ID, password and position from the Staff Database
strSQL = "Select * FROM Staff WHERE StaffID='" & txtLoginID.Text & "'And Password = '" & txtLoginPassword.Text & "'And Position='Administrator'"
'strSQL = "Select * FROM Staff WHERE Position='Administrator'"
sqlCnn = New SqlConnection(strConnectionString)
'Open Database Connection
sqlCnn.Open()
sqlCmd = New SqlCommand(strSQL, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader
If sqlReader.Read() Then
frmSales_Admin.Show()
Me.Hide()
Else
' If user enter wrong ID and password
' Throw an error message
MessageBox.Show("Incorrect User ID and Password..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ErrorCount = ErrorCount + 1
'Clear all fields
txtLoginID.Text = ""
txtLoginPassword.Text = ""
'Focus on login ID field
txtLoginID.Focus()
'If login was not successful at the first time, the user will only have two more Login attempts left
If (ErrorCount = 1) Then
lblNotify.Text() = "You have 2 login attempts left"
'If login was not successful for the second time, the user will only have one more Login attempts left
ElseIf (ErrorCount = 2) Then
lblNotify.Text() = "You have 1 login attempt left"
'If login was not successful for the third time, the user will not have anymore attempts left
ElseIf (ErrorCount = 3) Then
MessageBox.Show(" You have exceeded the maximum login attempts. System is now exiting. ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
'The system will then exit after the message box is closed
Application.Exit()
End If
End If
Catch ex As Exception
MessageBox.Show("Failed to connect to Database.", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub chkShowPassword_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowPassword.CheckedChanged
'To Make Password Visible
If chkShowPassword.Checked Then
txtLoginPassword.PasswordChar = ""
ElseIf chkShowPassword.Checked = False Then 'To make password not visible
txtLoginPassword.PasswordChar = "*"
End If
End Sub
Private Sub txtLoginID_DoubleClick(sender As Object, e As EventArgs) Handles txtLoginID.DoubleClick
txtLoginID.Clear()
txtLoginID.Focus()
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
'To go back to the main page of the app
frmSmartBookStore.Show()
Me.Hide()
End Sub
Private Sub txtLoginPassword_KeyDown(sender As Object, e As KeyEventArgs) Handles txtLoginPassword.KeyDown
Dim strConnectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim strSQL As String
'If user press enter key on password textbox
If e.KeyCode = Keys.Enter Then
If txtLoginPassword.Text = "" Or txtLoginID.Text = "" Then
MessageBox.Show("Please Enter your ID and Password.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' Both fields was supply
' Check if user exist in database
' Connect to Database
strConnectionString = "Data Source=LENOVO-PC; Initial Catalog=VB; Integrated Security=True"
Try
strSQL = "Select * FROM Staff WHERE StaffID='" & txtLoginID.Text & "'And Password = '" & txtLoginPassword.Text & "'And Position='Administrator'"
'strSQL = "Select * FROM Staff WHERE Position='Administrator'"
sqlCnn = New SqlConnection(strConnectionString)
'Open Database Connection
sqlCnn.Open()
sqlCmd = New SqlCommand(strSQL, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader
If sqlReader.Read() Then
frmSales_Admin.Show()
Me.Hide()
Else
' If user enter wrong username and password combination
' Throw an error message
MessageBox.Show("Incorrect User ID or Password..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ErrorCount = ErrorCount + 1
'Clear all fields
txtLoginID.Text = ""
txtLoginPassword.Text = ""
'Focus on login ID field
txtLoginID.Focus()
'If login was not successful at the first time, the user will only have two more Login attempts left
If (ErrorCount = 1) Then
lblNotify.Text() = "You have 2 login attempts left"
'If login was not successful for the second time, the user will only have one more Login attempts left
ElseIf (ErrorCount = 2) Then
lblNotify.Text() = "You have 1 login attempt left"
'If login was not successful for the third time, the user will not have anymore attempts left
ElseIf (ErrorCount = 3) Then
MessageBox.Show(" You have exceeded the maximum login attempts. System is now exiting. ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
'The system will then exit after the message box is closed
Application.Exit()
End If
End If
Catch ex As Exception
MessageBox.Show("Failed to connect to Database.", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End If
End Sub
'如果你有问题问我在[email protected]' 强大的文本 末级
通过使用=和/或<>运算符。 –