Amit Kumar
Amit Kumar

Reputation: 1059

An un-representable DateTime for year, Month and Day parameters in C#

I am facing some error like "Year, Month, and Day parameters describe an un-representable DateTime". But this problem is occurring when I am using another Database else the code which i have written is working fine.

For checking purpose I changed my PC Datetime from November to december than the same code in different database working fine. But I am not getting how to solve this bug. I have attached some code below where it is crashing.

var vi = session.Query<TJERecurMaster>().ToList();

                TJEShedulDetail tp = new TJEShedulDetail();

                var RecurMasterList = (from v in vi
                                       join jj in session.Query<TJEShedulDetail>()
                                       on v.FMasterID equals jj.TJERecurMaster.FMasterID into jjv
                                       from jv in jjv.DefaultIfEmpty(tp)
                                       where v.FInactive == false && v.TSCCompany == objCompany && v.TSCProperty == objProp
                                       orderby v.FDescription descending
                                       select new RecurMasterNotification
                                       {
                                           TSCompanyID = CompanyID,
                                           JeRecurDetail = v.TJERecurDetails,
                                           FMasterID = v.FMasterID,
                                           FScheduleID = jv.FScheduleID,
                                           FDescription = v.FDescription,
                                           FPostdate = jv.FLastNotification != null ? jv.FLastNotification : (v.FSetDay != 0 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, Convert.ToInt32(v.FSetDay)) : DateTime.Now),

                                           FAmount = v.TJERecurDetails.Where(P => P.FTranType == "D").Sum(P => P.FAmount)
                                                        - v.TJERecurDetails.Where(P => P.FTranType == "C").Sum(P => P.FAmount),
                                           FLastCreated = jv.FLastNotification,
                                           FNotes = v.FNotes,
                                           FJEType = v.FJEType,
                                           FInactive = v.FInactive,
                                           Frequency = jv.FFrequency,
                                           FDateSelection = jv.FDateSelection,
                                           FNextNotification = jv.FNextNotification,
                                           FLastCreatedString = jv.FLastNotification == jv.FNextNotification ? string.Empty : jv.FLastNotification.ToString(),
                                       }).ToList();

What is the solution for this type of error?

Upvotes: 0

Views: 2765

Answers (1)

Hans Kesting
Hans Kesting

Reputation: 39284

I expect the error occurs on this line:

FPostdate = jv.FLastNotification != null ? jv.FLastNotification : (v.FSetDay != 0 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, Convert.ToInt32(v.FSetDay)) : DateTime.Now)

and specifically this part:

new DateTime(DateTime.Now.Year, DateTime.Now.Month, Convert.ToInt32(v.FSetDay))

Here you create a new date from the current year and month, and some v.FSetDay. But what if that FSetDay is 31 in a month (like november) that only has 30 days? Or it has another illegal value like < 0 or > 31?

You need to decide how to handle that. Can you just ignore values that are higher than the number of days in this month? There is a DaysInMonth method that can be of use here.

Upvotes: 2

Related Questions