Datetime field with getdate() not auto generatinghttp://forums.asp.net/t/1771367.aspx/1?Datetime+field+with+getdate+not+auto+generatingSun, 26 Feb 2012 08:12:38 -050017713674840161http://forums.asp.net/p/1771367/4840161.aspx/1?Datetime+field+with+getdate+not+auto+generatingDatetime field with getdate() not auto generating <p>Hi,</p> <p>Does anyone know why I have a Datetime field set to getdate() by default in an SQL Server 2008 R2 database but it's inserting NULL. As far as I know thats all I need to do???</p> <p>I think I have a ghost in the machine!</p> <p>Cheers,</p> <p>Mike.</p> 2012-02-20T03:37:47-05:004840166http://forums.asp.net/p/1771367/4840166.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>it should work... how r u setting default value?</p> <p>try this</p> <p>ALTER TABLE TableName<br> ADD DEFAULT GETDATE() FOR ColumnName<br> go</p> <p>hope this helps...</p> 2012-02-20T03:42:34-05:004840183http://forums.asp.net/p/1771367/4840183.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>Thanks&nbsp;kedarrkulkarni,</p> <p>But i'm doing all this from inside VWD and my hosting company has a GUI for all of this too.</p> <p>I must have built 500 databases in my life and never had to CRUD on via SQL.</p> <p>Sorry,</p> <p>Mike.</p> 2012-02-20T03:53:32-05:004840233http://forums.asp.net/p/1771367/4840233.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>how are you inserting data in table row? r u seding the dataset updates from dataadater?</p> <p>u might have datetime field included in there....</p> <p>may be posting the code would help</p> 2012-02-20T04:35:30-05:004841896http://forums.asp.net/p/1771367/4841896.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>Its a WCF and Im now inserting the date with the other fields with the following code. I thought I could simply make the CommuniqueDate field by default set its self to the server date with getdate() when a new record is inserted but it doesnt seem to want to.</p> <pre class="prettyprint">public bool putCommunique(string communiqueIMEI, string communiqueType) { Communique communique = new Communique { CommuniqueIMEI = communiqueIMEI, CommuniqueType = communiqueType, CommuniqueDate = DateTime.Now }; _db.Communiques.AddObject(communique); _db.SaveChanges(); return true; }</pre> <p>Cheers,</p> <p>Mike.</p> 2012-02-20T21:46:15-05:004848675http://forums.asp.net/p/1771367/4848675.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>Hi bojangles,</p> <p>Have you tried to test that whether DateTime.Now has passed value to CommuniqueDate or not? You could use try...catch to catch that error.</p> <p>ObjectContext<span>.</span>AddObject Method :<a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx">http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx</a></p> <p>Thanks.</p> 2012-02-24T07:00:43-05:004851230http://forums.asp.net/p/1771367/4851230.aspx/1?Re+Datetime+field+with+getdate+not+auto+generatingRe: Datetime field with getdate() not auto generating <p>Thanks Chen Yu,</p> <p>Ive had a look through some Microsoft auto generated code, e.g.. the code generated for membership and the aspnet.mdf when you create a new MVC site and it looks like MS dont rely on SQL Server adding the date time automatically. So I will do it this way from now on.</p> <p>Cheers,</p> <p>Mike.</p> 2012-02-26T08:12:38-05:00