title: Format Exception: String not recognized as a valid datetime
body: <p>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:</p>
  
  <pre><code>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 &amp;&amp; n.BeginDate == b &amp;&amp; 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";
     }
  }
  </code></pre>
  
  <p>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.</p>