1

I am trying to call the GetMonths() method during the Row_Inserting() method but it shows me an error. Also, how do I copy the value of this method to a variable named Total_Pay?

public override bool Row_Inserting(OrderedDictionary rsold, ref OrderedDictionary rsnew) 
{      
    GetMonths(FROM_DATE, TO_DATE);
    return true;
}

public int GetMonths(DateTime FROM_DATE, DateTime TO_DATE)
{
    if (FROM_DATE > TO_DATE)
    {
        throw new Exception("Start Date is greater than the End Date");
    }

    int months = ((TO_DATE.Year * 12) + TO_DATE.Month) - ((FROM_DATE.Year * 12) + FROM_DATE.Month);

    if (TO_DATE.Day >= FROM_DATE.Day)
    {
        months++;
    }

    return months;
}
Draken
  • 3,134
  • 13
  • 34
  • 54
ghalib
  • 21
  • 1
  • 8
  • And what is wrong with this code? Please explain more. – Sami Kuhmonen May 02 '16 at 05:15
  • the calling GetMonths(FROM_DATE, TO_DATE); is wrong way to call this method – ghalib May 02 '16 at 05:16
  • Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1502: The best overloaded method match for 'ASP._Page_UNPAID_LEAVESadd_cshtml.cUNPAID_LEAVES_add.GetMonths(System.DateTime, System.DateTime)' has some invalid arguments Source Error: Line 27: GetMonths(FROM_DATE, TO_DATE); – ghalib May 02 '16 at 05:20
  • 1
    Could you please post declaration of FROM_DATE and TO_DATE? – madoxdev May 02 '16 at 05:22
  • these come from database with datetime datatype – ghalib May 02 '16 at 05:24
  • Do you need to cast them? – glenebob May 02 '16 at 05:31
  • 2
    As @KamilStachowiak has said, can we please see your declaration of the variables `FROM_DATE` and `TO_DATE` in your code? Could we also see where and how they are being set? – Draken May 02 '16 at 06:54
  • 1
    The two parameters in this line: `GetMonths(FROM_DATE, TO_DATE);` is *definitely not* of type `DateTime` or the compiler would've allowed the call. Show us the definition of those two parameters. – Lasse V. Karlsen May 02 '16 at 07:07

1 Answers1

0

Hope this helps..

    public override bool Row_Inserting(OrderedDictionary rsold, ref OrderedDictionary rsnew)
    {
        int Total_Pay;
        DateTime FROM_DATE = DateTime.Parse("02-May-2016"); //Replace with date you need
        DateTime TO_DATE = DateTime.Parse("08-May-2016"); //Replace with date you need
        Total_Pay = GetMonths(FROM_DATE, TO_DATE);
        return true;
    }

    public int GetMonths(DateTime FROM_DATE, DateTime TO_DATE)
    {
        if (FROM_DATE > TO_DATE)
        {
            throw new Exception("Start Date is greater than the End Date");
        }
        int months = ((TO_DATE.Year * 12) + TO_DATE.Month) - ((FROM_DATE.Year * 12) + FROM_DATE.Month);
        if (TO_DATE.Day >= FROM_DATE.Day)
        {
            months++;
        }
        return months;
    }

And checkout this similar question Link

Community
  • 1
  • 1
lal
  • 166
  • 4
  • A simple DateTime subtraction yields a TimeSpan, thus you'll have a straightforward way to get the months. Isn't that simpler? – Mario Vernari May 02 '16 at 07:48
  • Yes, but it's difficult to calculate the month with 31 days and the February of leap year etc. On second though ill explain ;), datetime subtraction – lal May 02 '16 at 08:00
  • On second though ill explain ;), DateTime subtraction give you difference in date you will need to calculate number of months by hand. The obvious choice is / 30 and for a count down purpose it's fine. but, when showing as difference between 2 dates we need to be more accurate. – lal May 02 '16 at 08:08
  • 1
    TimeSpan unfortunately does not implement Months or TotalMonths – or hor May 02 '16 at 08:10
  • Gotcha! I didn't check for the months property. – Mario Vernari May 02 '16 at 08:27