Solved

C# - Payroll/Getting Hours Worked thru time difference.

Posted on 2016-08-17
6
70 Views
Last Modified: 2016-09-05
Hello, I am working on a school project, it's a payroll system.
I have this current code that gets the difference of the TIME IN AND TIME OUT Of the user.

 try
{
    connection = new MySqlConnection(source);
    command = new MySqlCommand("Select DATEDIFF(TimeIN) - DATEDIFF(TimeOUT) FROM payroll.attendance WHERE Date BETWEEN '" + datefrom.Value.ToString("yyyy-MM-dd") + "' AND '" + dateto.Value.ToString("yyyy-MM-dd") + "' AND EmployeeID='" + lblid.Text + "'", connection);

    connection.Open();
    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        string diff = reader.GetString("DATEDIFF").ToString();
        MessageBox.Show(diff.ToString());
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Open in new window


The code is not yet working it says "incorrect parameter count in the call to native function datediff"

Another problem is that, It must get the time difference of all the dates under the user input from the datepicker.
So its like this "SELECT DATEDIFF FROM MYDATEBASE WHERE Date Between 08-10-2016 AND 08-17-2016"
From this, it must get all the timedifference of the date between the user input.
Heres the sample of my database(attendance)

Screenshot_20.png
0
Comment
Question by:Joven Canezal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 41759325
You are getting an error because DATEDIFF returns the difference between two fields:

https://msdn.microsoft.com/en-us/library/ms189794.aspx

The proper form for DATEDIFF is:
DATEDIFF ( datepart , startdate , enddate )

Open in new window

In your case it appears as if you want to use something like:
command = new MySqlCommand("Select DATEDIFF(minute, TimeIN, TimeOUT) FROM payroll.attendance WHERE Date BETWEEN '" + datefrom.Value.ToString("yyyy-MM-dd") + "' AND '" + dateto.Value.ToString("yyyy-MM-dd") + "' AND EmployeeID='" + lblid.Text + "'", connection);

Open in new window


-saige-
0
 

Author Comment

by:Joven Canezal
ID: 41759349
it_saige, I already tried that. but as stated above I get this error "incorrect parameter count in the call to native function datediff" I retried that code and still the same/
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points (awarded by participants)
ID: 41759384
Are you using SQL or MySql?

MySql does not require a datepart for the calculation: http://www.w3schools.com/sql/func_datediff_mysql.asp

Try:
command = new MySqlCommand("Select DATEDIFF(TimeIN, TimeOUT) FROM payroll.attendance WHERE Date BETWEEN '" + datefrom.Value.ToString("yyyy-MM-dd") + "' AND '" + dateto.Value.ToString("yyyy-MM-dd") + "' AND EmployeeID='" + lblid.Text + "'", connection);

Open in new window


-saige-
0
 

Author Comment

by:Joven Canezal
ID: 41759450
THANKS SIR! Got it :D seems fine now, I'm using mysql thanks for your tips
0
 
LVL 34

Expert Comment

by:it_saige
ID: 41784463
Answered authors question.

-saige-
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question