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

November 4th, 2010, 04:20 AM
|
Authorized User
|
|
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Riddle...
Hey everybody,
I'm building a database table bassed on the review table from the book.
I also have a 'createDate' and an 'UpdateDate' both of type 'datetime', which defaults to getdate().
BUT: when trying at runtime to add records, I get an error which states that I'm trying to put data of format 'datetime2' into a 'datetime' variable.
Now I changed the datatype to datetime2 for both variables and it seems to work, but the default values in the database are now set to 1-1-0001, how is this possible?getdate() should set the variable 'datetime' to the current date/time?
Bertus 
|

November 4th, 2010, 04:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Bertus,
How are you getting data from your application to the database? "Riddle" doesn't really describe the underlying situation very well.... ;-)
Imar
|

November 4th, 2010, 05:02 AM
|
Authorized User
|
|
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
hey imar,
i used an 'entitydatasource' connected to a detailsview which defaults to 'insert'.
the createdate is not in this detailsview, it should use 'getdate()' to get a date value
|

November 4th, 2010, 05:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Ah, I see. In this case, the date time values default to DateTime.MinValue (which is 1-1-0001) when Entity Framework creates an instance of your object.
The easiest thing to do is to create a partial class for your entity and assign the default values in the constructor. If you have an entity called Vacature, you can do something like this:
Code:
Public Partial Class Vacature
Public Sub New
CreateDateTime = DateTime.Now
UpdateDateTime = DateTime.Now
End Sub
End Class
Then when EF creates an instance, the constructor code gets called and the two properties get a default value. For new instances, this is exactly what you want. For existing instances, their value will be overridden by the value from the database.
Make sure your partial class is in the same namespace as your entity. For the Planet Wrox site, this would be something like PlanetWroxModel.
Hope this helps,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

November 4th, 2010, 05:37 AM
|
Authorized User
|
|
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
thanks imar! i thought i was going crazy over this
can I also use an 'oninserting' event handler?
|

November 4th, 2010, 05:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, you could, but that requires you to code this every time you insert something. By moving this code to constructor, you can fix this once and for all, regardless the code you use to insert the item. That is, it also works for manually code entities for example:
Dim myVacature As New Vacature() { ... }
context.AddToVacatures(myVacature)
context.SaveChanges()
Or anywhere else where your entities are instantiated.
Cheers,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

November 4th, 2010, 12:42 PM
|
Authorized User
|
|
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
riddle part two
grrrrr! I guess I need some more info about the class and the constructors:
Do I need to create a class in my App_code folder or in the code behind file?
And where do I find the exact name of my entity? is it the name of the table used in the .edmx file? Because I used that (the table's name is ILLREPORT) and in this table I have the following properties: reportDate and lastContactDate, I thought the Class must be looking something like this:
Code:
Imports skooterModel
Partial Public Class ILLREPORT
Public Sub New()
reportDate = DateTime.Now
lastContactDate = DateTime.Now
End Sub
end class
but reportDate doesn't show up in intellisense. I get the following error: 'reportDate' not declared. It may be inaccessible due to its protectionlevel.
|

November 4th, 2010, 01:15 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
In App_Code, as you're creating a site-wide solution.
Take a look at the model's designer for the namespace, or open the model's code behind (*.edmx. vb) and search for the word Namespace. Then wrap your partial class in that same namespace.
Cheers,
Imar
|

November 4th, 2010, 02:12 PM
|
Authorized User
|
|
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
No no, I allready found the namespace, it's 'skooterModel' as in my codeSnippet...I don't understand exactly what entity to use in Public Partial Class...
The .edmx file/form/page shows all the tables I created in my database, converted to the edmx file. These tables in the edmx files (entities I guess) have names, I thought I had to use this name for the Public Partial Class???
|

November 4th, 2010, 02:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You don't need an Imports statement (that brings the namespace into scope). Instead, you need to wrap the type in a namespace, which places it in that namespace.
E.g.
Code:
Namespace skooterModel
Partial Public Class ILLREPORT
...
End Class
End Namespace
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|
 |
|