格式异常:字符串未被识别为有效的日期时间

时间:2011-06-28 20:07:25

标签: c# .net

只要我给出Begin date和enddate的值,我的代码就可以正常工作。但是,当它们为空值时,它将返回格式异常。这就是我所做的:

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";
   }
}

使用try和catch没有用,因为我无法访问返回值。你可以告诉我如何处理这个错误。

6 个答案:

答案 0 :(得分:2)

在尝试将字符串转换为DateTimes

之前,您可以检查字符串是否为空
if (BeginDate == null || EndDate == null) {
    // handle it appropriately, perhaps by setting a default or returning
}

DateTime b = Convert.ToDateTime(BeginDate);
DateTime e = Convert.ToDateTime(EndDate);

如果可能,您还可以将方法更改为接受DateTime值而不是字符串

答案 1 :(得分:2)

DateTime不可为空。因此,尝试格式化Null或Empty的字符串应该返回异常。

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;

    String queryBeingDate = string.Empty;
    String queryEndDate = string.Empty;
    DateTime beginDateTime;
    DateTime endDateTime;
    if (DateTime.TryParse(beginDate, out beginDateTime))
    {
       queryBeingDate = beginDateTime.ToString("yyyy-MM-dd");
    }

    if(DateTime.TryParse(endDate, out endDateTime))
    {
       queryEndDate = 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 = queryBeingDate,
                     EndDate = queryEndDate,
                     n.BeginLATLONG,
                     n.EndLATLONG
                 });
}

答案 2 :(得分:1)

你从未说出你的字符串值是什么。它们的格式是否正确?

像这样放置try/catch例程:

void test(string BeginDate, string EndDate) {
  DateTime noDate = new DateTime(1900, 1, 1);
  DateTime b;
  DateTime e;
  try {
    b = Convert.ToDateTime(BeginDate);
  } catch (Exception error) {
    b = noDate;
    MessageBox.Show("Invalid Format: " + BeginDate);
  }
  try {
    e = Convert.ToDateTime(EndDate);
  } catch (Exception error) {
    e = noDate;
    MessageBox.Show("Invalid Format: " + EndDate);
  }
  if (e < b) {
    throw new Exception("End Date can not be before Begin Date.");
  }
}

答案 3 :(得分:0)

您可以将DateTime更改为Nullable,DateTime?或检查您使用string.IsNullOrEmpty()传入的字符串,并添加您认为最适合处理空值的任何逻辑。

答案 4 :(得分:0)

使用DateTime.TryParse()而不是Convert.ToDateTime()。从不抛出异常并返回一个bool来告诉你转换是否有效。

答案 5 :(得分:0)

替代

 DateTime b = Convert.ToDateTime(BeginDate);
    DateTime e = Convert.ToDateTime(EndDate);

 DateTime b, e;
  if(!DateTime.TryParse(BeginDate,out b))
      b = YOurDefaultBeginDate;
  if(!DateTime.TryParse(EndDate,out e))
      e = YOurDefaultEndDate;