Reputation: 1049
I am writing an application in c#.net that works with sql DB. I have 5 fields - DateTime. When i am in debug mode , i see that the DateTime fields are correct , but when i run the application i get the next exception - {System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.}
the code -
try
{
DateTime date = DateTime.MinValue;
date = AllEventsSortedList[0].dateTime;
foreach (Event m485 in _485List)
{
CycleTbl nCycle = new CycleTbl();
nCycle._485_DateTime = m485.dateTime;
nCycle.OrderNumber = m485.OrderNumber;
nCycle.Source = m485.Source;
nCycle.Dest = m485.Destination;
foreach (Event m412 in _412List)
{
if ((m485.dateTime.Year == m412.dateTime.Year) && (m485.dateTime.Month == m412.dateTime.Month) && (m485.dateTime.Day == m412.dateTime.Day))
nCycle._412_DateTime = (DateTime)m412.dateTime;
}
foreach (Event m500 in _500List)
{
if ((m485.dateTime.Year == m500.dateTime.Year) && (m485.dateTime.Month == m500.dateTime.Month) && (m485.dateTime.Day == m500.dateTime.Day))
nCycle._500_DateTime = (DateTime)m500.dateTime;
}
foreach (Event m502 in _502list)
{
if ((m485.dateTime.Year == m502.dateTime.Year) && (m485.dateTime.Month == m502.dateTime.Month) && (m485.dateTime.Day == m502.dateTime.Day))
{
nCycle._502_DateTime = m502.dateTime;
nCycle.LGVNumber = (int)m502.LGVNumber;
}
}
nCycle._502_DateTime = (DateTime)date;
LgvDB.CycleTbls.InsertOnSubmit(nCycle);
LgvDB.SubmitChanges();
}
}
catch (Exception ex)
{
MessageBox.Show("Error in building the table." + Environment.NewLine + ex.Message);
}
Can anyone tell me what could be the problem? thank you.
Upvotes: 2
Views: 3179
Reputation: 300539
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.).
January 1, 1753, through December 31, 9999
SQL Server 2008 introduced the datetime2 datatype which has a greater range in line with .NET DateTime.
Upvotes: 4