Imports System.Data.OleDb
Public Class Form4
Public cn As New OleDb.OleDbConnection
Public cmd As New OleDb.OleDbCommand
Public da As OleDbDataAdapter
Public dr As OleDbDataReader
Public ds As DataSet = New DataSet
Public sql As String = Nothing
Public ConString As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Settings.AppDB)
\'//---------------------------------------------------------------------------------------//\'
Public Sub myCn()
If cn.State = ConnectionState.Open Then cn.Close()
cn.ConnectionString = ConString
cn.Open()
End Sub
\'//---------------------------------------------------------------------------------------//\'
Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
\'TODO: This line of code loads data into the \'AddEditDeleteLogInDataSet.tblLoginHistory\' table. You can move, or remove it, as needed.
Me.tblLoginHistoryTableAdapter.Fill(Me.AddEditDeleteLogInDataSet.tblLoginHistory)
myCn()
LoadLname()
ListViewLog()
End Sub
\'//---------------------------------------------------------------------------------------//\'
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
If TextBox3.Text = "" Or TextBox4.Text = "" Then Exit Sub
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("SELECT Uname,Pword")
xSQL.AppendLine("FROM tblAdmin WHERE")
xSQL.AppendLine("Uname=@name AND")
xSQL.AppendLine("Pword=@word")
Try
Using cn As New OleDbConnection(ConString)
cn.Open()
Dim cmd As New OleDbCommand(xSQL.ToString, cn)
cmd.Parameters.AddWithValue("@name", TextBox3.Text)
cmd.Parameters.AddWithValue("@word", TextBox4.Text)
dr = cmd.ExecuteReader
If dr.HasRows Then
MsgBox("Sucessful!", MsgBoxStyle.Information)
LogHistory()
TextBox3.Text = ""
TextBox4.Text = ""
ListViewLog()
LoadLname()
Else
MsgBox("Invalid Username or Password!", MsgBoxStyle.Exclamation)
End If
dr.Close()
End Using
Catch ex As Exception
MessageBox.Show("ERROR:" & ex.Message.ToString, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
\'//---------------------------------------------------------------------------------------//\'
Private Function LoadLname()
Try
Using cn As OleDbConnection = New OleDbConnection(ConString)
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("SELECT * FROM tblLoginHistory")
xSQL.AppendLine("ORDER BY")
xSQL.AppendLine("ID DESC")
Dim cmd As New OleDbCommand(xSQL.ToString, cn)
Dim da As New OleDbDataAdapter
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds, "Uname")
Dim LastName As New AutoCompleteStringCollection
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
LastName.Add(ds.Tables(0).Rows(i)("Uname").ToString())
Next
TextBox3.AutoCompleteSource = AutoCompleteSource.CustomSource
TextBox3.AutoCompleteCustomSource = LastName
TextBox3.AutoCompleteMode = AutoCompleteMode.Suggest
End Using
Catch ex As Exception
MessageBox.Show("ERROR:" & ex.Message.ToString, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return ds
End Function
\'//---------------------------------------------------------------------------------------//\'
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
TextBox4.Text = ""
e.Handled = False
End If
End Sub
\'//---------------------------------------------------------------------------------------//\'
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("SELECT Uname,Pword")
xSQL.AppendLine("FROM tblLoginHistory")
xSQL.AppendLine("WHERE Uname = \'" & TextBox3.Text.Trim & "\' ")
Using cn As New OleDbConnection(ConString)
cn.Open()
Dim cmd As New OleDb.OleDbCommand(xSQL.ToString, cn)
dr = cmd.ExecuteReader
If dr.HasRows = True Then
While dr.Read()
TextBox4.Text = (dr("Pword").ToString)
End While
End If
cmd.Dispose()
\'dr.Close()
End Using
Catch ex As Exception
MessageBox.Show("ERROR:" & ex.Message.ToString, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
\'//---------------------------------------------------------------------------------------//\'
Private Function LogHistory()
Try
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("INSERT INTO tblLoginHistory")
xSQL.AppendLine("(Uname,Pword,Dtoday)")
xSQL.AppendLine("VALUES")
xSQL.AppendLine("(@name,@word,@today)")
Using cn As New OleDbConnection(ConString)
cn.Open()
Dim cmd As New OleDbCommand(xSQL.ToString, cn)
cmd.Parameters.AddWithValue("@name", TextBox3.Text)
cmd.Parameters.AddWithValue("@word", TextBox4.Text)
cmd.Parameters.AddWithValue("@date", DateTimePicker1.Text.Trim)
cmd.ExecuteNonQuery()
ListViewLog()
End Using
Return True
Catch ex As Exception
MessageBox.Show("ERROR:" & ex.Message.ToString, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
\'//---------------------------------------------------------------------------------------//\'
Private Function ListViewLog()
Try
ListView2.Items.Clear()
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("SELECT ID,Uname,Pword,Dtoday")
xSQL.AppendLine("FROM tblLoginHistory")
xSQL.AppendLine("ORDER BY ID")
Using cn As New OleDbConnection(ConString)
cn.Open()
Dim cmd As New OleDb.OleDbCommand(xSQL.ToString, cn)
dr = cmd.ExecuteReader
While dr.Read
Dim lv1 As New ListViewItem(dr.Item("ID").ToString.Trim)
lv1.SubItems.Add(dr.Item("Uname").ToString.Trim)
lv1.SubItems.Add(dr.Item("Pword").ToString.Trim)
lv1.SubItems.Add(dr.Item("Dtoday").ToString.Trim)
lv1.SubItems.Add("------------")
ListView2.Items.Add(lv1)
End While
End Using
cmd.Dispose()
dr.Close()
Return True
Catch ex As Exception
MessageBox.Show("ERROR:" & ex.Message.ToString, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
\'//---------------------------------------------------------------------------------------//\'
Private Sub btnLogHistory_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogHistory.Click
Me.ReportViewer2.RefreshReport()
End Sub
End Class