 |
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
|
|
|
|
|

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

March 2nd, 2012, 12:48 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
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
|
|

March 3rd, 2012, 01:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

March 3rd, 2012, 02:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

March 5th, 2012, 11:38 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
HI Imar,
is that complete code? how CreateDateTime can be found in that class?
Thanks
|
|

March 5th, 2012, 02:11 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

March 6th, 2012, 07:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Not sure whay it doesn't work for you. Sounds like you didn't have StoreGeneratedPattern set to Computed....
Cheers,
Imar
|
|

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