.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Calculate the hours worked by an employee

Posted By: srini     Posted Date: November 10, 2010    Points:2   Category :VB.Net
Hi. I am doing application on vb.net and sql server 2005 as database. Here i am using TimeIn and TimeOut textboxes to know the timing of an employee.Then i have another textbox to calculate hours worked. so i want to find the difference between TimeIn and TimeOut.

Eg:
TimeIn: 09.45
TimeOut:5.45

HoursWorked:8.00

Responses
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

Srini,

May be follwing may help you.


 if ( timeOut < timeIN)
{
hourWorked = timeOut+ 12 - timeIN;
}
else
{
hourWorked = timeOut - timeIN;
}


Regards,
Sajid P K

Author: srini             
Posted Date: November 11, 2010     Points: 5   

Hi Sajid,

That one is not working. I am using txtTimein.text for TimeIn and txtTimeOut for TimeOut and txtTotal.text for Totalhoursworked. That code shows error.

In SQL Server 2005,which type of datatype for TimeIn,TimeOut and TotalHoursWorked should be used?

Help me!

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   


Srini,

What is issue with this...?

In SQL Server 2005, There is no data type for time. I think you can use decimal for this..

Let me know if u have any question ?

Author: srini             
Posted Date: November 11, 2010     Points: 5   

ok Sajid. I will paste you a coding. You tell me,what to do.


Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer
Dim da As SqlDataAdapter
Dim ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=semicolon;Integrated Security=True")

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim stDate As String = dtp1.Text
Dim dat As Date
dat = dtp1.Value

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=semicolon;Integrated Security=True")
myCommand = New SqlCommand("insert into Date (Date,EmployeeID,EmployeeName,Designation,TimeIN,TimeOUT,Totalnoofhours) values( '" & dat & "','" & txtEmpid.Text & "','" & txtEmpname.Text & "','" & cbDesig.Text & "','" & txtTimein.Text & "','" & txtTimeout.Text & "','" & txtTotal.Text & "' )", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Data has been saved")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
txtEmpid.Text = ""
txtEmpname.Text = ""
cbDesig.Text = ""
txtTimein.Text = " "
txtTimeout.Text = ""
txtTotal.Text = " "
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Application.Exit()
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtEmpid.Text = ""
txtEmpname.Text = ""
cbDesig.Text = ""
txtTimein.Text = " "
txtTimeout.Text = ""
End Sub

Private Sub btnSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetting.Click
Setting.Show()
End Sub
End Class

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

srini,

I think u can store the time in , out , workerhour as deicmal / folat in Db.


Here no need to save the datattime in db.

I think this will help u...



Regards,
Sajid P K



Author: srini             
Posted Date: November 11, 2010     Points: 5   

ok Sajid. I will paste you a coding. You tell me,what to do.


Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer
Dim da As SqlDataAdapter
Dim ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=semicolon;Integrated Security=True")

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim stDate As String = dtp1.Text
Dim dat As Date
dat = dtp1.Value

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=semicolon;Integrated Security=True")
myCommand = New SqlCommand("insert into Date (Date,EmployeeID,EmployeeName,Designation,TimeIN,TimeOUT,Totalnoofhours) values( '" & dat & "','" & txtEmpid.Text & "','" & txtEmpname.Text & "','" & cbDesig.Text & "','" & txtTimein.Text & "','" & txtTimeout.Text & "','" & txtTotal.Text & "' )", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Data has been saved")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
txtEmpid.Text = ""
txtEmpname.Text = ""
cbDesig.Text = ""
txtTimein.Text = " "
txtTimeout.Text = ""
txtTotal.Text = " "
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Application.Exit()
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtEmpid.Text = ""
txtEmpname.Text = ""
cbDesig.Text = ""
txtTimein.Text = " "
txtTimeout.Text = ""
End Sub

Private Sub btnSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetting.Click
Setting.Show()
End Sub
End Class

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

srini,

I think u can store the time in , out , workerhour as deicmal / folat in Db.


Here no need to save the datattime in db.

I think this will help u...



Regards,
Sajid P K



Author: srini             
Posted Date: November 11, 2010     Points: 5   

Sajid,

Ok. Thats not a problem.

How to calculate the working hours by getting values from TimeIn and TimeOut.

Send the code.

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

Hi,

What is the data type of TimeIn and TimeOut ?


Andalso u r excepting .Net code or TSQL ?


Regards,
Sajid P K

Author: srini             
Posted Date: November 11, 2010     Points: 5   

Sajid,

Ok. Thats not a problem.

How to calculate the working hours by getting values from TimeIn and TimeOut.

Send the code.

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

Hi,

What is the data type of TimeIn and TimeOut ?


Andalso u r excepting .Net code or TSQL ?


Regards,
Sajid P K

Author: srini             
Posted Date: November 11, 2010     Points: 5   

Sajid,

Datatype for TimeIn,TimeOut,Hoursworked is varchar(10). Its quite working good.

Now i need vb.net code for calculating the hoursworked by an employee.

Help me!

Srini
Author: Sajid P K             
Posted Date: November 11, 2010     Points: 5   

Hi,

Please Check this...


   Dim timeOut As Double
Dim timeIN As Double
Dim hourWorked As Double

If timeOut < timeIN Then
hourWorked = timeOut + 12 - timeIN

Else
hourWorked = timeOut - timeIN

End If


Author: srini             
Posted Date: November 11, 2010     Points: 5   

Sajid,


Thanks for your code. How can we store the number of hours worked in TotalHoursWorked textbox.

Help me soon!

Srini
Author: srini             
Posted Date: November 11, 2010     Points: 5   

Sajid,


Thanks for your code. How can we store the number of hours worked in TotalHoursWorked textbox.

Help me soon!

Srini
Author: srini             
Posted Date: November 12, 2010     Points: 5   

This is the code to calculate total number of hours worked by an employee:


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim TimeA As Date
Dim TimeB As Date
Dim hh As Integer
Dim mm As Integer
If Not Date.TryParse(txtTimein.Text, TimeA) Then
End If
If Not Date.TryParse(txtTimeout.Text, TimeB) Then
End If
'Subtract (= time between)
hh = TimeB.Subtract(TimeA).Hours
mm = TimeB.Subtract(TimeA).Minutes
'ss = TimeB.Subtract(TimeA).Seconds
txtTotal.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ""

End Sub

Srini


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend