Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi need help guys ASAP.. i need to get the exact total number of hours listed in datagridview once i click the fill button ... the datagridview column header name is date,status, time
the values are


date=22/03/2018 date=22/03/2018
status= in status= out
time=7:30 time= 14:00

date=23/03/2018 date=22/03/2018
status=in status=out
time= 6:45 time = 17:00

the total number of hrs in two days always 16.. as you can see she was out at 2 oclock pm then have a 30 min late on morning..

the policy for their time
is 7am-5pm hours work = 8 hrs of work 2 hours are breaktym (not counted)
morning rendered time = 3.5
afternoon rendered time = 4.5
total = 8 hrs
.
.
how can i get the exact total time of work of employee ?i think a have an error of this code.

What I have tried:

If txtID.Text = "" Then
        MsgBox("insert Employee ID")

    ElseIf txtID.Text <> "" Then
        Try
            totalMins = 0
            conn.Open()
            query = New MySqlCommand("SELECT No, Name, date, status, position,TIME_FORMAT(STR_TO_DATE(time,  '%l:%i:%s %p'), '%H:%i %p') AS time,HOUR(TIME_FORMAT(time, '%H:%i ')) AS hour,MINUTE(TIME_FORMAT(time,'%H:%i')) AS minute From cash_table WHERE No=" & txtID.Text & " AND (date BETWEEN '" & DateTimePicker2.Value.Date & "' AND '" & DateTimePicker3.Value.Date & "')", conn)
            dr = query.ExecuteReader()
            If dr.HasRows Then
                Do While dr.Read()
                    'Console.WriteLine(dr.GetInt32(0) & vbTab & dr.GetString(1))
                    txtName.Text = dr.GetString("Name")
                    txtPosition.Text = dr.GetString("position")

                    arr(0) = dr.Item("date")
                    arr(1) = dr.Item("status")
                    'arr(2) = dr.Item("position")
                    arr(2) = dr.Item("time")
                    DataGridView1.Rows.Add(arr)

                    stat = dr.Item("status")
                    timeHr = dr.Item("hour")
                    timeMin = dr.Item("minute")

                    If (stat = "IN") Then
                        lateMin = 0
                        extMin = 0

                        If (timeHr > 7) Then
                            lateMin += (timeHr - 7) * 60

                        End If

                        If (timeHr >= 7) Then
                            If (timeMin > 0) Then
                                lateMin += (timeMin)
                            End If
                        End If
                    ElseIf (stat = "OUT") Then
                        If (timeHr > 17) Then
                            extMin += (timeHr - 17) * 60
                        End If

                        If (timeHr >= 17) Then
                            If (timeMin >= 15) Then
                                extMin += (timeMin)
                            End If
                        End If

                        If (timeHr > 17) Then
                            extMin += (3.5 - (timeHr - 13)) * 60 + timeMin - 16

                        End If
                        totalMins += (480 - lateMin) + (extMin)
                        totalMins = Val(totalMins) - Val(16)
                    End If
                Loop
            Else
                MsgBox("No Records Found!", MsgBoxStyle.Information, "Search")
            End If
            dr.Dispose()

            TextBox4.Text = Math.Floor(totalMins / 60) & ":" & (totalMins Mod 60)

        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            dr.Dispose()
            conn.Close()

        End Try

        If txtID.Text = "" Then
            DataGridView1.Rows.Clear()
            txtName.Clear()
            txtPosition.Clear()
        End If

    End If
Posted
Updated 22-Mar-18 13:18pm
Comments
Richard MacCutchan 22-Mar-18 9:31am    
Use proper DATETIME types so you can easily calculate accurate time intervals.
CaThey Joy Galias 22-Mar-18 11:29am    
Sample pls im only a beginner for this code

You do have errors in that code: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

That's not the problem you've noticed, but it needs fixing throughout your whole app before you go any further ... and we have no idea what the problem you have noticed is. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
CaThey Joy Galias 22-Mar-18 11:28am    
The query is run according to flow the only problem is.. The computation...
What is the proper code for it
Quote:
i think a have an error of this code.

What happen that you do not expect ?
-----
VB
query = New MySqlCommand("SELECT No, Name, date, status, position,TIME_FORMAT(STR_TO_DATE(time,  '%l:%i:%s %p'), '%H:%i %p') AS time,HOUR(TIME_FORMAT(time, '%H:%i ')) AS hour,MINUTE(TIME_FORMAT(time,'%H:%i')) AS minute From cash_table WHERE No=" & txtID.Text & " AND (date BETWEEN '" & DateTimePicker2.Value.Date & "' AND '" & DateTimePicker3.Value.Date & "')", conn)

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900