Skip to content
Advertisement

SQLDateTime OverFlow with Correct DateTime Format

below is my code snippet where I insert a DateTime value to SQL database and encountered the following error:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

deliveryCart.DeliveryDate = (DateTime)row.DeliveryDate;

Log.SaveLog(ConfigurationManager.AppSettings["LogFilePath"] + @"Log.txt", "deliveryCart.DeliveryDate: " + deliveryCart.DeliveryDate.ToString());
//*value retrieved is 12/31/2019 12:00:00 AM

dataContext.Q_TBL_SUBMITTED_DELIVERY_CARTs.InsertOnSubmit(deliveryCart);

May I know if there is a format to be used? when converting the value of row.DeliveryDate?

Advertisement

Answer

actually the problem is SQL DateTime =/= C# Datetime

you need to change 2 things

  • Database change the field type from DateTime to DateTime2

  • Query you need to be explicit

you can find futher informations here,here and here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement