0

HI all,

I am passing the date parameter as like this:

 DateTime date = DateTime.Now;
  date.ToString("YYYY-MM-DD 00:00:00:000");

But getting this exceptions:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753        12:00:00 AM and 12/31/9999 11:59:59 PM. at System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value) at System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value)
5
  • stackoverflow.com/questions/468045/… Commented Dec 31, 2010 at 5:45
  • how do you pass the paramtere; using querystring of command object ? Commented Dec 31, 2010 at 5:45
  • Can you post the code you use to persist the datetime to the database, because that is where the error would be. If you can actually also tell us what line of the code you pOst has the error, we will be able to fix this sooner Commented Dec 31, 2010 at 5:46
  • The "code" you posted doesn't do anything. Please post the actual code that talks to the database (or a reasonable substitute). The .ToString() method won't generate a SQL exception. Commented Dec 31, 2010 at 5:46
  • Your .ToString() format is all wrong - you need to be more careful with your format string! Those characters are case-sensitive! You need to use .ToString("yyyy-MM-dd") - small y and d and capital M Commented Dec 31, 2010 at 7:29

3 Answers 3

2

There should be a decimal dot between the seconds and the milliseconds. The format string is case sensitive. Try:

date.ToString("yyyy-MM-dd")

or

date.ToString("yyyy-MM-dd HH:mm:ss.fff")

Also ask yourself whether you really need to convert arguments to strings. It smells odd and it may not be necessary. If you want to pass only the date and not the time, then pass the Date property of your DateTime object as your parameter value. Keep it strongly-typed to avoid SQL-injection, performance and type conversion issues.

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

Comments

0

No parameter is being passed in here, the code sample you have posted is incomplete by looking at the resulting SqlTypeException

Also the date format should be:

date.ToString("yyyy-MM-dd HH:mm:ss:fff")

1 Comment

Philip Fourie->Hi still getting that exception
0

If you are trying to ignore the time portion (hence your zeros) try

date.ToString("yyyy-MMM-dd");

If you want the time portion too ...

date.ToString("yyyy-MMM-dd hh:mm:ss.fff tt");

Note both have 3 Ms for the month which makes them unambiguous strings that SQL should be able to parse and cannot misinterpret.

But, why not just pass the value as a date object rather than convert to a string?

3 Comments

This answer is basically identical to one already posted. Why post this over 2 years later?
didn't check the date! Actually is different. Everyone always insists in having months as 2 characters in strings. Use 3 characters, that way it is unambiguous, if you have to have strings at all of course.
Mentioning something in your answer about why you added one extra "M" would be kinda important...

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.