Avatar of Joven Canezal
Joven Canezal

asked on 

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

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)

User generated image
MySQL ServerC#

Avatar of undefined
Last Comment
it_saige
Avatar of it_saige
it_saige
Flag of United States of America image

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-
Avatar of Joven Canezal
Joven Canezal

ASKER

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/
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Joven Canezal
Joven Canezal

ASKER

THANKS SIR! Got it :D seems fine now, I'm using mysql thanks for your tips
Avatar of it_saige
it_saige
Flag of United States of America image

Answered authors question.

-saige-
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo