Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi all,

 

I'm creating a timeclock program, and having a problem calculating the total time that an employee works. I have a form that retrieves the employees information from the database and fills a dataset. The dataset is then displayed in a datagrid. The fields that are displayed are the Date, Start Time, End Time, and Hours worked. What I want to do is calculate the Total Hours worked and display it in a textbox. How can I do that? My code is as follows:

 

' Attempt to retrieve the data from the database based on the SSN entered and the date range
           Try
               strSQL = "SELECT TimeIn.DateIn, Format([TimeIn.StartTime], 'hh:mm') as StartTime, Format([TimeIn.EndTime], 'hh:mm') as EndTime, Format([TimeIn.EndTime]-[TimeIn.StartTime], 'hh:mm') as HoursWorked "
               strSQL = strSQL & "FROM Employees INNER JOIN TimeIn ON Employees.EmpID = TimeIn.EmpID "
               strSQL = strSQL & "WHERE (((TimeIn.SSN)=" & "'" & strEmployee & "'" & ") AND ((TimeIn.DateIn) Between #" & strStart & "# And #" & strEnd & "#));"

               strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & "\timeclock.mdb"

               Dim myCnn As New OleDbConnection(strConnection)
               Dim myDA As New OleDbDataAdapter(strSQL, myCnn)
               myCnn.Open()

               ' Fill the dataset with the data retrieved from the database
               Dim ds As New DataSet
               myDA.Fill(ds, "TimeIn")

               ' Populate the datagrid with the information in the dataset
               dgTimeView.DataSource = ds
               dgTimeView.DataMember = "TimeIn"

               ' Close the connections, and set all values to nothing
               myCnn.Close()
               myCnn.Dispose()
               myDA.Dispose()
               ds.Dispose()
               strSQL = ""

               ' If there is a problem with the data retrieved from the DB, display the error
               ' information in Label1 so the user will know what happened
           Catch ex As Exception
               Dim strMessage As String
               strMessage = ex.Message.ToString
               strMessage = strMessage & "  " & ex.ToString

               Label1.Text = strMessage

           End Try

 

Thanks in advance,

 

Chris

Edited by PlausiblyDamp
if(computer.speed == "slow")
    {  
       hamster.feed();  
    }
if(computer.speed == "really slow")
    {  
        hamster.kill();
        BuyNewHamster();
    }

  • *Experts*
Posted

First, I wouldn't convert the DateTime values from the database. I'd return them as regular DateTime fields.

 

To get the hours/minutes worked, you'll need to get the values of start and end date into DateTime variables from your dataset. You can then Subtract them and get a TimeSpan object which will show you the hours (and minutes, etc.):

DateTime startDate = (DateTime)ds.Tables["TimeIn"].Rows[0]["StartTime"];
DateTime endDate = (DateTime)ds.Tables["TimeIn"].Rows[0]["EndTime"];
TimeSpan diff = endDate.Subtract(startDate);
Debug.WriteLine(diff.Hours);

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
Ok, but how would I do it in VB.Net?
if(computer.speed == "slow")
    {  
       hamster.feed();  
    }
if(computer.speed == "really slow")
    {  
        hamster.kill();
        BuyNewHamster();
    }

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...