I found this code for users attempt 3 times and gets lock
I want to lock the user of having access to the website like lock user adding 0 and 1
Imports System.Data.SqlClient
Imports System.Data
Imports System.Drawing
Partial Class VBCode
Inherits System.Web.UI.Page
Private attempts As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
attempts = Convert.ToInt32(ViewState("attempts"))
Dim ds As New DataSet()
Dim ds1 As New DataSet()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select userid,attemptcount from userinformation where username=@username", con)
cmd.Parameters.AddWithValue("@username", txtUsername.Text)
cmd.Parameters.AddWithValue("@password", txtPwd.Text)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
If ds IsNot Nothing Then
If ds.Tables(0).Rows.Count > 0 Then
attempts = Convert.ToInt32(ds.Tables(0).Rows(0)("attemptcount"))
If attempts = 3 Then
lblMsg.Text = "Your Account Already Locked"
lblMsg.ForeColor = Color.Red
Else
cmd = New SqlCommand("select userid,attemptcount from userinformation where username=@username and password=@password", con)
cmd.Parameters.AddWithValue("@username", txtUsername.Text)
cmd.Parameters.AddWithValue("@password", txtPwd.Text)
da = New SqlDataAdapter(cmd)
da.Fill(ds1)
If ds1 IsNot Nothing Then
If ds1.Tables(0).Rows.Count > 0 Then
ViewState("attempts") = ds1.Tables(0).Rows(0)("attemptcount")
If Convert.ToInt32(ViewState("attempts")) <> 3 Then
cmd = New SqlCommand("update userinformation set attemptcount=0 where username=@username and password=@password", con)
cmd.Parameters.AddWithValue("@username", txtUsername.Text)
cmd.Parameters.AddWithValue("@password", txtPwd.Text)
cmd.ExecuteNonQuery(lblMsg.Text = "Logged in Successfully.")
lblMsg.ForeColor = Color.Green
Else
lblMsg.Text = "Your Account Already Locked...Contact Administrator"
lblMsg.ForeColor = Color.Red
End If
Else
Dim strquery As String = String.Empty
If attempts > 2 Then
strquery = "update userinformation set islocked=1, attemptcount=@attempts where username=@username and password=@password"
lblMsg.Text = "You Reached Maximum Attempts. Your account has been locked"
Else
attempts = attempts + 1
ViewState("attempts") = attempts
strquery = "update userinformation set attemptcount=@attempts where username=@username"
If attempts = 3 Then
lblMsg.Text = "Your Account Locked"
Else
lblMsg.Text = "Your Password Wrong you have only " & (3 - attempts) & " attempts"
End If
End If
cmd = New SqlCommand(strquery, con)
cmd.Parameters.AddWithValue("@username", txtUsername.Text)
cmd.Parameters.AddWithValue("@password", txtPwd.Text)
cmd.Parameters.AddWithValue("@attempts", attempts)
cmd.ExecuteNonQuery(lblMsg.ForeColor = Color.Red)
End If
End If
End If
Else
lblMsg.Text = "UserName Not Exists"
lblMsg.ForeColor = Color.Red
End If
End If
con.Close()
End Using
End Sub
End Class