c# - how to convert date time if passing null value in date field -
how store null value , how insert null values in database? im getting error.
string not recognized valid datetime.
if (taskobj.estimatedtime == null) { taskobj.estimatedtime = convert.todatetime(estimatetimelabel.text); } public datetime estimatedtime { set { estimatedtime = value; } { return estimatedtime; } }
you need specify in database can allow null specific column.
if have database can run like:
alter column `yourcolumn` datetime2() default null
this allow use null in database.
you need convert datetime in code nullable type. otherwise .net comes out default date 1/1/0001 12:00:00 (not helpful).
there 2 ways this. recommend first it's purely down coding style.
you can either using ? operator:
public datetime? estimatedtime { set { estimatedtime = value; } { return estimatedtime; } }
or nullable generic function:
public nullable<datetime> estimatedtime { set { estimatedtime = value; } { return estimatedtime; } }
don't forget convert estimatedtime field nullable type (again either using ? operator or nullable.
i hope helps. if not leave me comment , best assist you.
Comments
Post a Comment