Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all. Thanks for reading my post. I developed a "time clock" to keep track of hours worked. I have the hours worked for the day. I need to get the total hours for each employee. I am hitting a wall. I have two calendar controls to select a date range. I am able to display the info. from the database into a gridview which includes daily hours and the employee's name. How can I get the total hours for each employee for the selected date range? They don't have to go into the gridview. I could display names and total hours using labels placed below the gridview. Here is the code I am using to get the data into the gridview.

VB
Protected Sub Calendar332_SelectionChanged(sender As Object, e As EventArgs)
        'declare SQL statement that will query the database
        Dim sqlstring As String = SELECT firstname, lastname, startdate, starttime, endtime, hours FROM hoursworked WHERE startdate =  Calendar332.SelectedDate  AND startdate = Calendar333.SelectedDate   ORDER By startdate

        Dim strSQLconnection As String =  Source=SQL Server;SERVER=123abc; Initial Catalog=abc; UID=xxxxxxx;PWD=xxxxxxx;

        Dim sqlConnection As New Data.SqlClient.SqlConnection(strSQLconnection)
        Dim sqlCommand As New Data.SqlClient.SqlCommand(sqlstring, sqlConnection)
        sqlConnection.Open()

        Dim reader As Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader()

        

        GridView7.Visible = True
        GridView7.DataSource = reader
        GridView7.DataBind()
        reader.Close()
    End Sub



Thank you for any input you might be able to provide.

D-Bar


Table structure:
id - auto increment
first name
last name
startdate
starttime
endtime
hours

Data is test data like:
John Doe 2013-11-15 2013-11-15 8:00:00 AM 2013-11-15 5:00:00 PM 9:00
Posted
Updated 15-Nov-13 3:46am
v2
Comments
Sampath Lokuge 15-Nov-13 8:54am    
Can you give us your table structure for the employee and the sample data for that ?
Foothill 15-Nov-13 12:30pm    
You could always use datediff to get the difference for each entry then sum them up.
D-Bar 21-Nov-13 8:21am    
Thank you Foothill. Sum was the keyword there. I had a column in database with daily minutes and I used The SUM as you suggested to get total minutes and then converted to hours and minutes.

D-Bar
rk_2010 15-Nov-13 21:55pm    
WHERE startdate = Calendar332.SelectedDate AND startdate = Calendar333.SelectedDate

Ensure the fields are correct. Because in the Where condition you have given both as StartDate.

Replace:
VB
Dim sqlstring As String = SELECT firstname, lastname, startdate, starttime, endtime, hours FROM hoursworked WHERE startdate =  Calendar332.SelectedDate  AND startdate = Calendar333.SelectedDate   ORDER By startdate

with:
VB
Dim sqlstring As String = "SELECT firstname, lastname, startdate, starttime, endtime, hours" & vbcr & _
                           "FROM hoursworked" & vbcr & _
                           "WHERE startdate BETWEEN  '" & Calendar332.SelectedDate & "' AND '" & Calendar333.SelectedDate "'" & vbcr & _
                           "ORDER By startdate"


For further information, please see: String Data Type (Visual Basic)[^]

[EDIT]
SQL
SELECT firstname + ' ' +  lastname AS [Employee], SUM(DATEDIFF(day, endtime, starttime))
FROM hoursworked
WHERE startdate BETWEEN @firstdate AND @seconddate
GROUP BY firstname + ' ' +  lastname
ORDER BY firstname + ' ' +  lastname

[/EDIT]
 
Share this answer
 
v2
Comments
D-Bar 18-Nov-13 12:00pm    
Thanks to everyone who responded to my question. Unfortunately, nothing worked. I was getting the same results which I already had, total hours for the day, not total for all days selected.

D-Bar
Maciej Los 18-Nov-13 13:01pm    
Please, see my answer again (after [EDIT]) ;)
D-Bar 21-Nov-13 8:26am    
Thank you Maciej Los. That was what I needed. I messed it up by adding the other values I needed to the select statement and that seemed to throw things off. When I worked with the query as is I was able to get the total hours with the SUM keyword.

D-Bar
Maciej Los 21-Nov-13 10:42am    
You're welcome ;)
it will help

checkout the example 1 and 2
 
Share this answer
 

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



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