Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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 19th, 2013, 05:59 PM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default Database default value

Hi Folks, I am trying to enter a default value (todays date) in my database by setting (getdate()) in the default value or binding in the Date column properties - but it is not running.

I am using a details view with a associated sqldata source and i have generated the usual insert / delete commands.

Any ideas of where I am going wrong,

Thanks
Mark
 
Old February 19th, 2013, 07:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you define "it's not running"? Do you get an error?

It's important to realize that defaults only work when your SQL statement is not supplying a value. So when you've set up your data controls for that date column, SQL will take the value supplied by the control, not it's default.

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!
The Following 2 Users Say Thank You to Imar For This Useful Post:
markhh (February 20th, 2013)
 
Old February 20th, 2013, 03:55 PM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi Imar, what I mean is that when I insert a new record the date field which I am trying to insert automatically using (getdate()) is not inserted - no errors are reported and the field remains null. What about using an event like ItemInserting would this be another way around it,

Regards
Mark
 
Old February 20th, 2013, 04:31 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It sounds like your UI is still supplying a value, even though it's null. A default only works when you're not supplying a value (even a null value) for the column with the default. So, let's say you have a table with an Id, a Name and a CreationDate, the following would trigger the default on CreationDate:

INSERT INTO SomeTable (Id, Name) VALUES (1, 'Something')

but this wouldn't:

INSERT INTO SomeTable (Id, Name, CreationDate) VALUES (1, 'Something', null)

If you have a data control with a parameter for CreationDate, ASP.NET sends null to the database which results in null, an not the result of GetDate() to be inserted.

Either remove the column / parameter from your data control, or replicate the default value in your code in one of the data events by supplying a value explicitly.

Hope this helps,

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 February 21st, 2013, 09:22 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thanks again Imar, some very good points.

Regards
Mark
 
Old April 25th, 2013, 04:38 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi Imar, your solution works fine (removing the field in question from the sqldatasource).

Another query I have searched but I cannot find how to insert a value or text into my database using one of the data events (such as additional comments). Any ideas,

Regards
Mark
 
Old April 25th, 2013, 04:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Mark,

I don't understand the question. Can you elaborate?

Also, can you please start a new thread for each new topic?

Thanks,

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
Code for Chpt.2-Wrox File Share is missing Default.aspx and Default.aspx.vb bzoni BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 October 23rd, 2009 02:39 PM
Cannot open user default database KamalRaturi ASP.NET 2.0 Professional 2 October 25th, 2006 10:43 AM
DTS Package - Default database error interlogics SQL Server DTS 0 December 9th, 2005 06:30 AM
ODBC Default Database peace Access VBA 0 September 25th, 2004 04:45 PM
ODBC Default Database peace Access 0 September 25th, 2004 04:32 PM





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