如何解决“String未被识别为有效的DateTime”?

时间:2021-10-14 10:40:02

I'm converting a text field into date time and inserting into db. However, when the text field is null, it throws an exception that

我正在将文本字段转换为日期时间并插入到db中。但是,当text字段为null时,它会抛出异常

String was not recognized as a valid DateTime

字符串未被识别为有效的DateTime

This is what I did:

这就是我做的:

    nvpt4Entities db = new nvpt4Entities();

    ClassInfo order = new ClassInfo
    {
        ClassID = classID.Text,
        ClassName = className.SelectedItem.Text,
        ClassTime = classTime1.Text,
        ClassDate = DateTime.ParseExact(classDate1.Text, "MM/dd/yyyy", null),
        ClassDay = classDay.SelectedValue,
        ClassMonth = classMonth.SelectedValue,
        ClassLocation = classLocation.SelectedItem.Text,
        ClassNotes= classNotes.Text,
        ClassInstructor = classInstructor.SelectedValue,
        ClassInstructor1 = classInstructor2.SelectedValue,
        ClassInstructor2 = classInstructor3.SelectedValue,
        MultiDate2 = DateTime.ParseExact(multidate2.Text, "MM/dd/yyyy", null),
        MultiDate3 = DateTime.ParseExact(multidate3.Text, "MM/dd/yyyy", null),
        MultiDate4 = DateTime.ParseExact(multidate4.Text, "MM/dd/yyyy", null),
        MultiDate5 = DateTime.ParseExact(multidate5.Text, "MM/dd/yyyy", null),
        ClassStrength =  Convert.ToInt16(classStrength.Text),
        ClassProvider = classProvider.SelectedItem.Text,
        LocationID = Convert.ToInt16(classLocation.SelectedValue),
        ClassCID = Convert.ToInt16(className.SelectedValue),
        ProviderID = Convert.ToInt16(classProvider.SelectedValue)

    };

So, I want to know how to handle null strings to be converted to datetime while inserting into db

所以,我想知道在插入db时如何处理要转换为datetime的空字符串

2 个解决方案

#1


3  

This has nothing to do with the database. Pay attention to the error message and where the exception originates. The only thing of relevance is where the error comes from:

这与数据库无关。请注意错误消息以及异常发生的位置。唯一相关的是错误来自:

DateTime.ParseExact(__, "MM/dd/yyyy", null)

Replace this with an appropriate method call that handles the "empty/invalid string" case, however is appropriate, or use validation to ensure this step is never reached; DateTime? should be used for nullable columns, a sentinel can be used otherwise. (See Mark Byers' answer for what the contents of said method may look like.)

将此替换为处理“空/无效字符串”情况的适当方法调用,但这是合适的,或使用验证来确保永远不会达到此步骤;约会时间?应该用于可空列,否则可以使用标记。 (参见Mark Byers关于所述方法内容的答案。)

Happy coding.

#2


2  

If your fields are nullable DateTimes you can use this:

如果您的字段是可以为空的DateTimes,您可以使用:

ClassDate = string.IsNullOrEmpty(classDate1.Text) ? (DateTime?)null :
                DateTime.ParseExact(classDate1.Text, "MM/dd/yyyy", null),

#1


3  

This has nothing to do with the database. Pay attention to the error message and where the exception originates. The only thing of relevance is where the error comes from:

这与数据库无关。请注意错误消息以及异常发生的位置。唯一相关的是错误来自:

DateTime.ParseExact(__, "MM/dd/yyyy", null)

Replace this with an appropriate method call that handles the "empty/invalid string" case, however is appropriate, or use validation to ensure this step is never reached; DateTime? should be used for nullable columns, a sentinel can be used otherwise. (See Mark Byers' answer for what the contents of said method may look like.)

将此替换为处理“空/无效字符串”情况的适当方法调用,但这是合适的,或使用验证来确保永远不会达到此步骤;约会时间?应该用于可空列,否则可以使用标记。 (参见Mark Byers关于所述方法内容的答案。)

Happy coding.

#2


2  

If your fields are nullable DateTimes you can use this:

如果您的字段是可以为空的DateTimes,您可以使用:

ClassDate = string.IsNullOrEmpty(classDate1.Text) ? (DateTime?)null :
                DateTime.ParseExact(classDate1.Text, "MM/dd/yyyy", null),