0

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?

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using dateTime.toString(), then need to ensure that the string is formatted per:

deliveryCart.DeliveryDate.ToString("yyyy-MM-ddTHH:mm:ss.fffffff");

Also, DateTime is a value type and not a reference type. So, it cant be null. Value types if not initialised have a value of 0. For Datetime, it can be DateTime.MinValue which is outside Sql Server's value.

1 Comment

You shouldn't pass that date value as string to SQL, but as (the DateTime value of) a parameter of some date type. So no formatting needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.