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 that’s all I need to do???
It’s a WCF and I’m 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 doesn’t seem to
want to.
I’ve 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 don’t rely on SQL Server adding the date time automatically. So I will do it this way
from now on.
Cheers,
Mike.
Marked as answer by Chen Yu - MSFT on Feb 27, 2012 05:56 AM
bojangles
Participant
974 Points
642 Posts
Datetime field with getdate() not auto generating
Feb 20, 2012 03:37 AM|LINK
Hi,
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 that’s all I need to do???
I think I have a ghost in the machine!
Cheers,
Mike.
kedarrkulkar...
All-Star
34167 Points
5490 Posts
Re: Datetime field with getdate() not auto generating
Feb 20, 2012 03:42 AM|LINK
it should work... how r u setting default value?
try this
ALTER TABLE TableName
ADD DEFAULT GETDATE() FOR ColumnName
go
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
bojangles
Participant
974 Points
642 Posts
Re: Datetime field with getdate() not auto generating
Feb 20, 2012 03:53 AM|LINK
Thanks kedarrkulkarni,
But i'm doing all this from inside VWD and my hosting company has a GUI for all of this too.
I must have built 500 databases in my life and never had to CRUD on via SQL.
Sorry,
Mike.
kedarrkulkar...
All-Star
34167 Points
5490 Posts
Re: Datetime field with getdate() not auto generating
Feb 20, 2012 04:35 AM|LINK
how are you inserting data in table row? r u seding the dataset updates from dataadater?
u might have datetime field included in there....
may be posting the code would help
KK
Please mark as Answer if post helps in resolving your issue
My Site
bojangles
Participant
974 Points
642 Posts
Re: Datetime field with getdate() not auto generating
Feb 20, 2012 09:46 PM|LINK
It’s a WCF and I’m 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 doesn’t seem to want to.
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; }Cheers,
Mike.
Chen Yu - MS...
All-Star
21584 Points
2493 Posts
Microsoft
Re: Datetime field with getdate() not auto generating
Feb 24, 2012 07:00 AM|LINK
Hi bojangles,
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.
ObjectContext.AddObject Method :http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx
Thanks.
Feedback to us
Develop and promote your apps in Windows Store
bojangles
Participant
974 Points
642 Posts
Re: Datetime field with getdate() not auto generating
Feb 26, 2012 08:12 AM|LINK
Thanks Chen Yu,
I’ve 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 don’t rely on SQL Server adding the date time automatically. So I will do it this way from now on.
Cheers,
Mike.