The conversion of a varchar data type to a datetime data type resulted in an out-of-range value error

前端 未结 3 1468
春和景丽
春和景丽 2020-12-21 04:55

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value error

I\'m trying to enter data into my table using a form, the date

3条回答
  •  执笔经年
    2020-12-21 05:03

    Basically, you shouldn't be passing the DateTime values as strings in your SQL at all. Use parameterized SQL, and just set the parameter value directly. You should always use parameterized SQL as far as possible:

    • It gives better code/data separation
    • It avoids problematic conversions (like this one)
    • It avoids SQL injection atttacks

    Additionally, your exception handling is pointlessly complex. Just use a using statement, and let the SqlException bubble up directly - why bother wrapping it in a vanilla Exception?

提交回复
热议问题