Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub CmdAdminLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdminLogin.Click
    If Len(Trim(TxtAdminUserName.Text)) = 0 Then
        MessageBox.Show("Please enter user name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        TxtAdminUserName.Focus()
        Exit Sub
    End If
    If Len(Trim(TxtAdminPassword.Text)) = 0 Then
        MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        TxtAdminPassword.Focus()
        Exit Sub
    End If



    Try
        Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\DbCPDRAS.accdb;Persist Security Info=False;")
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
        cn.Open()
        Dim dr1 As OleDbDataReader
        Dim com As New OleDbCommand
        com.CommandText = "select [UserID],[Pass] from AdminInfo where UserID = @UName"

        ' UserName
        Dim UName As OleDbParameter = New OleDbParameter("@UName", OleDbType.VarWChar, 20)
        UName.Value = UCase(TxtAdminUserName.Text.ToString())
        com.Parameters.Add(UName)
        com.Connection = cn

        dr1 = com.ExecuteReader
        If dr1.Read Then
            If UCase(dr1("Pass")) = UCase(TxtAdminPassword.Text) Then
                cn.Close()
                CBformState.SelectedItem = "Admin"

                Dim obj As New FrmMain
                Me.Hide()
                obj.Show()
            Else
                Static Counter As Integer = 0
                MsgBox("Invalid Password   " & 2 - Counter & " Attempts left!", vbExclamation, "Login")

                TxtAdminPassword.Text = ""
                TxtAdminUserName.Text = ""
                Counter += 1
                If Counter = 3 Then Me.Close()
                cn.Close()
                LinkLabel2.Visible = True
                LinkLabel2.Text = "Forget Password"
                Return

            End If
        Else
            Static Counter1 As Integer = 0
            MsgBox("Invalid UserName and Password   " & 2 - Counter1 & " Attempts left!", vbExclamation, "Login")
            TxtAdminPassword.Text = ""
            TxtAdminUserName.Text = ""
            Counter1 += 1
            If Counter1 = 3 Then
                Me.Timer1.Interval = 1

                Me.Timer1.Start()

                CmdAdminLogin.Enabled = False

                TxtAdminUserName.Enabled = False

                TxtAdminPassword.Enabled = False

                If Me.Timer1.Interval = 3 Then

                    Me.Timer1.Stop()

                    MessageBox.Show("Login Available", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    CmdAdminLogin.Enabled = True

                    TxtAdminUserName.Enabled = True

                    TxtAdminPassword.Enabled = True
                    cn.Close()
                End If

                Return
            End If

        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
Posted
Updated 5-Mar-14 2:52am
v2

1 solution

For single application session:
Keep a class level integer that holds number of tries. On each failure increase this number by 1. When 3 tries are over, lock the account in database and add a condition do display appropriate message.

For multi-session:

On each failure, update failed attempts in database for the user. If this number goes above 3, display account locked message.

Update: Here is an outline on how to implement this. It is in C# but logic remains same in VB as well.

In single application session:

C#
private int _failedAttempts;

        protected void LoginButtonClick(object sender, EventArgs e) {

            // Try user login here

            if (success)
            {
                // process ahead
            }
            else {
                _failedAttempts++;
            }

            if (_failedAttempts == 3) {

                // Update database for this user. Set account locked out.

                MessageBox.Show("Your account has been locked out.");
            }

        }



Multiple session:

C#
private int _failedAttempts;

protected void LoginButtonClick(object sender, EventArgs e)
{

    // User table should have a failed attempt count field.

    // Check if value of that field is 3
    if (valueIs3)
    {
        MessageBox.Show("Your account has been locked out.");
    }
    else
    {
        // Try user login here

        if (success)
        {
            // set failed attempt field value as 0

            // process ahead
        }
        else
        {
            _failedAttempts = failedAttemptCountValueFromDatabase;
            // add one to failed attempts field

            _failedAttempts++;
        }

        if (_failedAttempts == 3)
        {

            // Update database for this user. Set account locked out.

            MessageBox.Show("Your account has been locked out.");
        }
    }

}
 
Share this answer
 
v2
Comments
Krunal Rohit 5-Mar-14 9:00am    
You're right !!
I'd have shown this through code, but don't know about VB.

-KR
dan!sh 5-Mar-14 9:04am    
I only work with VB when threatened at gun point. Or when I have save my job. Otherwise, no.
Member 10617989 5-Mar-14 10:24am    
give me some example of code ..
dan!sh 5-Mar-14 10:50am    
Updated the reply. I don't do VB, so it is in C#.
Member 10617989 5-Mar-14 19:10pm    
thanks men!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900