Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2012, 12:53 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile utc date time

Hello Imar,

While storing review I was thinking that why use always system date time using getdate() and not the utc date. For this I changed default value to getutcdate() in both fields CreateDateTime and UpdateDateTime in Reviews table and updated the edmx file, and removed the lines which explicitly stores the date time for review.

Review myReview;
if (_id == -1) // Insert new item
{
myReview = new Review();
//myReview.CreateDateTime = DateTime.Now;
//myReview.UpdateDateTime = myReview.CreateDateTime;

myEntities.AddToReviews(myReview);
}

But it was giving error that datetime2 type value can not be converted into datetime type value. So I changed datatype of both date time field to datetime2(7) and it is working well now.

The only problem is that it is storing default date time value 1/1/0001 12:00 AM and not the real utc date. Please tell me how to store real utc date values

Thanks
 
Old February 25th, 2012, 03:39 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

use UtcNow instead of Now:

Code:
if (_id == -1) // Insert new item
{
myReview = new Review();
myReview.CreateDateTime = DateTime.UtcNow;
myReview.UpdateDateTime = myReview.CreateDateTime;
myEntities.AddToReviews(myReview);
}
datetime2 type is used to store datetime values with no limitation of range values. for example in Hijri date we use 1391/12/05 today instead of 2012/Mar/24 so in earlier versions we could not store date values and we was forced to convert our date values to Gregorian dates. but now there is no limitation with datetime2.
__________________
happy every time, happy every where

Reza Baiat
 
Old February 25th, 2012, 09:03 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Quote:
use UtcNow instead of Now:
I'm aware of this fact, but I do not want to set the date in code behind file. Any other method?

Thanks
 
Old February 26th, 2012, 11:44 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Please someone help me...
 
Old February 28th, 2012, 11:46 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi there

GetUtcDate() in sql returns the UtcTime. it works fine as it should. but I think there are some problems with edmx update. I've faced some problems with edmx updating, and I was forced to reCreate the edmx file.
__________________
happy every time, happy every where

Reza Baiat
 
Old February 28th, 2012, 02:07 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi Reza,
I did not delete and recreated edmx file, but yes deleted the corresponding table, saved edmx and then added again that table and saved edmx but it did not work.

How this problem can be solved?
Thanks
 
Old February 29th, 2012, 04:07 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi there
I think CreateDateTime and UpdateDateTime fields of your table are not nullable. so when Entity Framework wants to set the values, uses it's .Net default values (new DateTime()). Try to set these two fields as nullable, set default value to GetUtcDate(), update edmx file and then test below codes

Code:
if (_id == -1) // Insert new item
{
myReview = new Review();
myEntities.AddToReviews(myReview);
}
__________________
happy every time, happy every where

Reza Baiat
 
Old February 29th, 2012, 05:24 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

No Luck,
Null is being stored in table
 
Old March 1st, 2012, 02:36 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi Imar, Kindly say something on this topic.
 
Old March 1st, 2012, 02:37 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Does it work when you add a record in the table directly?

I would add a constructor to the class and set the value from there in code.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Time Field not displaying Date and Time webmeister Access 2 January 31st, 2011 12:49 AM
converting date format from utc time in seconds to GMT murali XSLT 8 March 10th, 2009 07:56 AM
date and time in EST time zone anboss XSLT 1 May 21st, 2008 01:42 PM
Local Date and Time to UTC? dbayona SQL Server 2005 3 July 24th, 2007 10:12 PM
time (UTC v local and NOW()) m3rajk PHP Databases 2 August 18th, 2003 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.