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 March 1st, 2012, 03:31 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thanks for quick reply.
Quote:
Does it work when you add a record in the table directly?
Yes it is working well, if I am inserting record directly in table.

Now what next?

Last edited by sophia; March 1st, 2012 at 03:44 AM..
 
Old March 2nd, 2012, 12:48 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi Imar,
what you were saying about constructor, how it will solve the problem, I do not want to use DateTime.UtcNow; in code behind

Thanks
 
Old March 3rd, 2012, 01:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you set the date using a constructor, each time the time an instance of the class is created (by you, or by EF or by other code) the date gets a default UTC date. Then when the item is retrieved from the database the value gets overwritten.

This gives you an easy default value while you can still overwrite it.

Not sure why this doesn't work through the designer though.

Cheers,

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!
 
Old March 3rd, 2012, 01:01 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Then would you write constructor as below

Code:
private Review()
{
   using(PlanetWroxEntities myEntities = new PlanetWroxEntities())
   {
       myEntities.CreateDateTime = DateTime.UtcNow;
    }
}
if this is so, then why not write this directly on save button click's code?
Quote:
Then when the item is retrieved from the database the value gets overwritten.
value gets overwritten when item is retrieved from database or written to database?

Many thanks
 
Old March 3rd, 2012, 02:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

This code targets the entire context which doesn't make that much sense. Instead you want to target a single instance of a review. Just add something like this to your App_Code folder:

Code:
 
namespace PlanetWroxModel
{
  public partial class Review
  {
    public Review()
    {
      this.CreateDateTime = DateTime.UtcNow; 
    }
  }
}
This sets the CreateDateTime when a new instance is created.

Quote:
value gets overwritten when item is retrieved from database or written to database?
When retrieved from the database. If you query an old object from the database, you want its original CreateDateTime, not today's date.

Cheers,

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!
 
Old March 5th, 2012, 11:38 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

HI Imar,
is that complete code? how CreateDateTime can be found in that class?

Thanks
 
Old March 5th, 2012, 02:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
is that complete code?
Did you try it out? That would be the best way to get that question answered.

Note the partial keyword in the class declaration....

Cheers,

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!
 
Old March 6th, 2012, 01:53 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Quote:
Did you try it out?
yes I tried it, but I used a different name for the class file being generated and then modified the code (class name and constructor name) inside that file manually, perhaps that's why it was not working.

Well, it is working now.

But here also we are using UTCNow somehow. why it is giving error if we insert date through function getutcdate().

Many thanks
 
Old March 6th, 2012, 07:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not sure whay it doesn't work for you. Sounds like you didn't have StoreGeneratedPattern set to Computed....

Cheers,

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!
 
Old March 6th, 2012, 10:14 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Quote:
Not sure whay it doesn't work for you
is it working on your side without writing constructor?

It is not working with StoreGeneratedPattern set to Computed too.





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.