Groups | Blog | Home
all groups > asp.net > april 2008 >

asp.net : inserting date created with new record


dean.h.brown@gmail.com
4/24/2008 9:24:03 AM
I'm trying to set the date created when I add a new record: (using a
FormView)

This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString
%>"
InsertCommand="INSERT INTO [AdultEvent] ([DateCreated],
[DateLastModified], [EventName])
VALUES ('04/23/2008', '04/23/2008', @EventName)"

but I want the dates to be the current date and time.

I've tried variations on this:
VALUES ('<%$ DateTime.Now.ToString('MM/dd/yyyy') %>', '04/23/2008',
@EventName)"

But I get:
System.Data.SqlClient.SqlException: Incorrect syntax near 'MM'.

Or:
VALUES ('<%$ DateTime.Now %>', '04/23/2008', @EventName)"

System.Data.SqlClient.SqlException: Conversion failed when converting
Jeff Dillon
4/24/2008 4:57:38 PM
How about GetDate()

Transact-SQL

But I would recommend against direct sql inserts like that. Look up "SQL
Injection"

Jeff

[quoted text, click to view]

dean.h.brown@gmail.com
4/30/2008 5:49:37 AM
On Apr 24, 7:57 pm, "Jeff Dillon" <jeffdil...@hotmailremove.com>
[quoted text, click to view]

I don't think SQL injections apply here - there's no chance to change
AddThis Social Bookmark Button