0

I have a method which takes in some parameters, among which there are three DateTime fields (populated from the Web Form).

The method uses an automatically generated TableAdapter and DataSet to add a new record onto a table. After the Table.Add[newRow] statement, we call the Dataset.Update(Table) statement to persist the addition onto the database.

While this works as expected for almost all cases, it sometimes gives an error: System.Data.SqlClient.SqlException (0x80131904): The conversion of a date data type to a datetime data type resulted in an out-of-range value The statement has been terminated..

Does anyone have a clue as to what may be happening?

4

2 に答える 2

2

From here

The date datatype can hold dates from Jan 1 of year 1 thru Dec 31, 9999. The datetime datatype can only hold dates from Jan 1, 1753 thru Dec 31, 9999. So you must have dates in your table with values of less than Jan 1, 1753.

Since DateTime is a struct, hence always has a value(DateTime.MinValue), another possible reason is that the DateTime could not be parsed successfully.

So you could check before you insert if the DateTime is in a valid range of SqlDateTime:

DateTime darkAge = new DateTime(1111, 1, 1);
if (darkAge >= SqlDateTime.MinValue.Value && darkAge <= SqlDateTime.MaxValue.Value)
{
    // we're not gettting here since the dark age was before 1753 
}
于 2013-01-11T11:24:33.493 に答える
0

はい、SQLDateTimeとDotNetDateTimeは許可された範囲で異なります。

SqlDateTime.MinValue!= DateTime.MinValue、なぜですか?

于 2013-01-11T11:17:53.597 に答える