I have an application that shows the shift target of a certain reference. I have a lot of references divided in 3 shifts (morning, afternoon, evening) The app is working good but now i want to reset the counter of the shift targets in the beginning of every shift for all references. I mean, when the clock is 6am, all the morning shift should put the counter on 0. I have the following code but is not working. Something is missing and i'm stuck.
Dim rtime As New TimeSpan(0, 0, CInt(requestddate.TimeOfDay.TotalSeconds))
hora1 = New TimeSpan(6, 0, 0)
hora2 = New TimeSpan(14, 0, 0)
hora3 = New TimeSpan(22, 0, 0)
ConnectDatabase()
If rtime = hora1 Then
count = 0
Try
With cmd4
.Connection = conn
.CommandText = "Select ShiftTarget.IDShiftTarget from ShiftTarget, turnos, linhas WHERE ShiftTarget.IDLinha = linhas.IDLinha and ShiftTarget.IDTurno = turnos.IDTurno And turnos.Descricao = 'Manha' And linhas.NomeLinha = '" & GlobalVariables.linha & "'"
End With
While objReader.Read()
listaIDs.Add(objReader("IDShiftTarget").ToString())
End While
objReader.Close()
DisconnectDatabase()
ConnectDatabase()
With cmd1
.Connection = conn
.CommandText = "INSERT into contador (IDShiftTarget, Data, Contador) VALUES (@ID, @date, @cont)"
For Each item As String In listaIDs
MsgBox(item)
.Parameters.AddWithValue("@ID", item)
.Parameters.AddWithValue("@date", data.ToString("yyyy-MM-dd"))
.Parameters.AddWithValue("@cont", count)
.ExecuteNonQuery()
Next
End With
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End If
End Try
DisconnectDatabase()
Can someone tell me what am i doing wrong?
Update:
This is what i get (and it's expected) from the select query:

And this is the table where i want to reset the counter:

So, imagine, when the actual time is 6am, i want to insert something like:
IDContador IDShiftTarget Data Contador
11933 32 2016-02-23 0
11934 19 2016-02-23 0
11935 20 2016-02-23 0
11936 35 2016-02-23 0
I cannot simply reset to 0 where idshifttarget is equal to 19,20,32,35 because that would reset all the records, and i want to keep them in the table. I need to check the MAX(ID) where the shiftTarget is the value i got in the array, and then put the Contador to 0.
UPDATE:
I have made this changes in the code:
While objReader.Read()
listaIDs.Add(objReader("IDShiftTarget").ToString())
End While
objReader.Close()
DisconnectDatabase()
ConnectDatabase()
With cmd1
.Connection = conn
.CommandText = "INSERT into contador (IDShiftTarget, Data, Contador) VALUES (@ID, @date, @cont)"
For Each item As String In listaIDs
MsgBox(item)
.Parameters.AddWithValue("@ID", item)
.Parameters.AddWithValue("@date", data.ToString("yyyy-MM-dd"))
.Parameters.AddWithValue("@cont", count)
.ExecuteNonQuery()
Next
End With
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End If
End Try
DisconnectDatabase()
With this code i can only see the first ID and in the console i get the next error: Exception thrown: 'System.InvalidOperationException' in MySql.Data.dll
TimeSpanwhich represents the remaining hours/minutes/seconds to the "next" 6am time shift. Additionally create aTimerwhoseIntervalis set to theTimeSpan.TotalMilliseconds. In the Timer Tick Event Handler execute your Inserts. After that set the Timer Interval to 24 hours. Proceed with the other shifts accordingly.If objReader.Read = True Thenwill lead to that the first row of your query is ignored! 2nd: Both in your Select and Insert statements you concat parameters in the WHERE/VALUES clause. Please use prepared statements with bound parameters to avoid SQL Injection.