I am developing a biometric daily monitoring system and I need to check if a Mysql table row has already been inserted with data based on the date.
I found this post, Limiting the time in and time out in a day in VB.NET? But i can'f figure out how to use it in my code.
The flow is, the first login of an employee for the day will be amIn
, the next is amOut
, next is pmIn
, and last is pmOut
.
LOGIC :
If 'amIn' is not NULL
then insert data to 'amOut'
else if 'amOut' is not NULL
then insert data to 'pmIn'
else if 'pmIn' is not NULL
then insert data to 'pmOut'
VB.NET code
#CONNECTION STRING HERE
conn.Open()
Dim sqlCommand As New MySqlCommand
Dim empDate As New DateTime(Convert.ToInt32(empYear.Text), Convert.ToInt32(empMonth.Text), Convert.ToInt32(empDay.Text), Convert.ToInt32(empHour.Text), Convert.ToInt32(empMin.Text), Convert.ToInt32(empSec.Text))
Dim str_insertAttendance As String
Dim reader As MySqlDataReader
Try
sqlCommand.Connection = conn
str_insertAttendance = "select count(*) from attendance where empid=@id and date=@date and amin=@amIn and amout=@amOut and pmin=@pmIn and pmout=@pmOut)"
sqlCommand.CommandText = str_insertAttendance
reader = sqlCommand.ExecuteReader()
If reader.Read() Then
If reader.IsDBNull(0) Then
str_insertAttendance = "insert into attendance values (@id, @date, @amIn, @amOut, @pmIn, @pmOut)"
sqlCommand.CommandText = str_insertAttendance
sqlCommand.Parameters.AddWithValue("@id", Convert.ToInt32(empID.Text))
sqlCommand.Parameters.AddWithValue("@date", empDate)
sqlCommand.Parameters.AddWithValue("@amIn", empDate)
sqlCommand.Parameters.AddWithValue("@amOut", "")
sqlCommand.Parameters.AddWithValue("@pmIn", "")
sqlCommand.Parameters.AddWithValue("@pmOut", "")
sqlCommand.ExecuteNonQuery()
End If
End If
Return True
conn.Close()
Catch ex As Exception
Return False
MsgBox("Error occured: Could not insert record")
End Try
TABLES
CREATE TABLE `employee` (
`empID` int(11) NOT NULL,
PRIMARY KEY (`empID`)
);
CREATE TABLE `attendance` (
`empID` int(11) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`amIn` datetime DEFAULT NULL,
`amOut` datetime DEFAULT NULL,
`pmIn` datetime DEFAULT NULL,
`pmOut` datetime DEFAULT NULL,
KEY `empID` (`empID`),
CONSTRAINT `attendance_ibfk_1` FOREIGN KEY (`empID`) REFERENCES `employee` (`empID`)
);