c# - The conversion of a datetime2 data type to a datetime data type resulted in out-of-range value -
i'm using entity framework in visual studio 2012 developing c# program. want add together records in database table . record (object) includes attribute (transaction_date) doesn't allow null values , it's datetime format. in database i'm aiming format:
yyyy-mm-dd hh:mm:ss.fff
so want pass current date , time it, code this:
newentry.trasaction_date = datetime.parseexact(datetime.now.tostring(), "yyyy-mm-dd hh:mm:ss.fff", cultureinfo.invariantculture);
but it's giving me error:
the conversion of datetime2 info type datetime info type resulted in out-of-range value.
i want know why it's not converting format want?
constraints:
you shouldn't think database storing values in particular string format @ - more stores numbers decimal or hex.
instead, should view date/time, e.g.
// todo: want local time, rather utcnow? // todo: alter transact_date transactiondate ideally, follow // .net naming conventions newentry.transact_date = datetime.now;
when retrieve value, should datetime
@ point too. then if want display value user can apply particular format. different users may want different formats - , may want display same date/time in different time zones.
it's of import differentiate between intrinsic info store (in case date/time) , text format happens used display 1 particular context/application. should avoid conversions to/from strings @ time when don't need one. ideally, these should @ boundaries of application - e.g. when displaying text user, or potentially serializing json or xml. whenever api allows not perform conversion (e.g. database parameter) should avoid it.
as current error - possible it's different field you're not populating, , hence using default(datetime)
, out of range? create lot of sense - whereas datetime.now
shouldn't out of range, unless you've either applied separate constraint, or scheme clock miles out.
c# datetime
No comments:
Post a Comment