this is the event table that records all the status(IN/OUT):
+--------------------------------------------------+-----------------+
| Event_ID | User_BannerID | Group_ID | Timestamp | Status | Creator|
+----------+----------------+----------+-----------+--------+--------+
| | | | | | |
| | | | | | |
+----------+----------------+----------+-----------+--------+--------+
So am making a sign in-out program using vb.net and mysql. i was wondering if theres a code i can put in somewhere that will be like if a user opens the application he/she must signin before they can signout and if they open the application and they’ve already signed in, they cant signin again they must signout before they can sign out again.
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmMain
Private myConnString As String
Private myUserID As String
Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myConnString = value
End Set
End Property
Public WriteOnly Property UserID() As String
Set(ByVal value As String)
myUserID = value
End Set
End Property
Private Sub refreshStatus(ByRef statusView As DataGridView)
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name, ug.group_name AS Class, DATE_FORMAT(e.timestamp,'%b %d %Y - %r')AS DateTime, e.status AS Status " _
& "FROM event e, user u, user_group ug " _
& "WHERE(e.user_bannerid = u.user_bannerid) " _
& "AND e.group_id = ug.group_id " _
& "AND ug.user_bannerid = ?userID " _
& "AND event_id IN " _
& "( " _
& "Select MAX(e.event_id) " _
& "FROM event e " _
& "GROUP BY e.user_bannerid " _
& ") " _
& "ORDER BY datetime"
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myCommand.Parameters.Add("?userID", myUserID)
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
dgvStatus.DataSource = myData
dgvStatus.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboStatus.Items.Add("In")
cboStatus.Items.Add("Out")
cboStatus.SelectedIndex = 0
dgvStatus.ReadOnly = True
refreshStatus(dgvStatus)
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT ug.group_id, ug.group_name " _
& "FROM attendance.user_group ug " _
& "WHERE user_bannerid = ?userID and level_id is NULL "
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myCommand.Parameters.Add("?userID", myUserID)
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
cboClass.DataSource = myData
cboClass.DisplayMember = "group_name"
cboClass.ValueMember = "group_id"
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
conn.ConnectionString = myConnString
myCommand.Connection = conn
myCommand.CommandText = "INSERT INTO event(user_bannerid, group_id, timestamp, status, creator)" _
& "VALUES(?UserID, ?GroupID, NOW(), ?Status, ?Creator)"
myCommand.Parameters.Add("?UserID", myUserID)
myCommand.Parameters.Add("?GroupID", cboClass.SelectedValue)
myCommand.Parameters.Add("?Status", cboStatus.SelectedItem)
myCommand.Parameters.Add("?Creator", myUserID)
Try
conn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Status Successfully Updated")
Catch myerror As MySqlException
MsgBox("There was an error updating the database: " & myerror.Message)
End Try
refreshStatus(dgvStatus)
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Dim oForm As frmLogin
oForm = New frmLogin()
frmLogin.Show()
oForm = Nothing
Me.Hide()
End Sub
Private Sub frmLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Dim oForm As frmLogin
oForm = New frmLogin()
frmLogin.Show()
oForm = Nothing
Me.Hide()
End Sub
End Class
Why not just automatically sign the user out if they are already signed in before signing them in again? You can just tell them (through a messagebox or something) that they have been automatically signed out of their previous session if you discover that they were previously signed in.
Update
In reviewing your question and code, I think that you are making life far too difficult for yourself by relying on the activity log to provide the user status. The activity log should just be for recording discrete activities, not for determining the user’s status.
What I recommend is that you add a status field to the user table. This will always store the user’s current status, in or out.
When the user opens your application, you will have a simple check: what is the user’s status? If the user is logged in, show the user a Log Out button. If the user is not logged in show them a Log Out button. Don’t let them choose from the dropdown what their status is because I can guarantee they will mess something up.
If the user is logged in, but wants to log in again, they can press the log out button. You will update the user table with their new status and record the activity, then disable the Log Out button and enable the Log In button. When they press the Log In button, you will update the user status and record the new activity.
As an option, if the user is currently logged in when they start the app, you can show them (from the activity log or as a separate field in the user record) the last time they logged in so they are not confused about why they are being presented with the logout button.
Update with specific code