My code works fine as far as I give the values for Begin date and enddate. However, when they are null values, it returns the format exception. This is what I did:
public static string CheckInsertRecord(String EventType, String BeginDate, String EndDate)
{
NCDCPoint ncdc = new NCDCPoint();
CEOSurveyDataContext CDC = new CEOSurveyDataContext();
int et = Convert.ToInt32(EventType);
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime b = Convert.ToDateTime(BeginDate);
DateTime e = Convert.ToDateTime(EndDate);
var query = (from n in CDC.NCDCPoints
where n.EVENT_TYPE_ID == et && n.BeginDate == b && n.EndDate == e
select new {
n.EVENT_TYPE_ID,
BeginDate = n.BeginDate.ToString("yyyy-MM-dd",provider),
EndDate = n.EndDate.ToString(),
n.BeginLATLONG,
n.EndLATLONG
});
if (query.Any())
{
return new JavaScriptSerializer().Serialize(query.ToList());
}
else
{
return "No duplicate";
}
}
Putting try and catch was not useful as I cannot access the return values. Can u please let me know how to deal with this error.
DateTimeis not nullable. So trying to format a string that is Null or Empty should return an exception.