inserting date created with new record

Discussion in 'ASP .Net' started by dean.h.brown@gmail.com, Apr 24, 2008.

  1. Guest

    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
    datetime from character string
    , Apr 24, 2008
    #1
    1. Advertising

  2. Jeff Dillon Guest

    How about GetDate()

    Transact-SQL

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

    Jeff

    <> wrote in message
    news:...
    > 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
    > datetime from character string
    Jeff Dillon, Apr 25, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 24, 7:57 pm, "Jeff Dillon" <>
    wrote:
    > How about GetDate()
    >
    > Transact-SQL
    >
    > But I would recommend against direct sql inserts like that. Look up "SQL
    > Injection"
    >


    I don't think SQL injections apply here - there's no chance to change
    this value by external means
    , Apr 30, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt Emich

    Inserting A New Record

    Curt Emich, May 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    320
    Curt Emich
    May 6, 2004
  2. Nick
    Replies:
    0
    Views:
    508
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,220
    Michael Borgwardt
    May 30, 2004
  4. Replies:
    1
    Views:
    389
    sengsational
    Oct 13, 2007
  5. Replies:
    1
    Views:
    220
Loading...

Share This Page