Home All Groups Group Topic Archive Search About

Default DateTime Value in a column

Author
25 Jan 2006 4:37 PM
Josh
Hi,

Im creating an in memory DataTable.   I have a "dtCreated" column that I
want to be populated with the current date and time when a record is added.
I dont want to code this every time I add a record.

Is there a way to set the defaultValue of the column so that is always
inserts the current date time when a record is added.

I want this to work like a SQL Server column that has "getdate()" as the
default value.

Thanks in advance...

Author
25 Jan 2006 6:22 PM
Miha Markic [MVP C#]
Perhaps you might consider implementing DataTable.RowChanged event and
initialize default values there.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Josh" <s@a.com> wrote in message
news:%23u0lh2cIGHA.524@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> Im creating an in memory DataTable.   I have a "dtCreated" column that I
> want to be populated with the current date and time when a record is
> added. I dont want to code this every time I add a record.
>
> Is there a way to set the defaultValue of the column so that is always
> inserts the current date time when a record is added.
>
> I want this to work like a SQL Server column that has "getdate()" as the
> default value.
>
> Thanks in advance...
>
Author
25 Jan 2006 6:30 PM
Cor Ligthert [MVP]
Josh,

You mean something as

mycolumn.defaultvalue = Now

Not tested and as it works, than it gives for sure the datetime at
construction time.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassdefaultvaluetopic.asp

I hope this helps,

Cor
Author
26 Jan 2006 12:06 AM
David Veeneman
One simple way to do it is to add it to your table definition. Set
AllowNulls to false, and set the default value of the column to 'getdate()'
(no quotes). The current date will be written to the column of any new
record created.

--
David Veeneman
Foresight Systems

AddThis Social Bookmark Button