locked
Insert Date? RRS feed

  • Question

  • User446063609 posted

    I have looked around on these forms seeing answers to this question but most are doing DB inserts a bit differently.  Is there a simple way to insert the date into an Insert Statement?  Below is my code and the unsuccessful method I tried:

    <asp:AccessDataSource ID="AccessDataSource1" runat="server"

    DataFile="~/App_Data/Tributes.mdb"

    DeleteCommand="DELETE FROM [Tributes] WHERE [ID] = ?"

    InsertCommand="INSERT INTO [Tributes] ([Date], [Name], [Email], [Organization], [Tribute]) VALUES (?, ?, ?, ?, ?)"

    SelectCommand="SELECT * FROM [Tributes]"

    UpdateCommand="UPDATE [Tributes] SET [Date] = ?, [Name] = ?, [Email] = ?, [Organization] = ?, [Tribute] = ? WHERE [ID] = ?">

    <DeleteParameters>

    <asp:Parameter Name="ID" Type="Int32" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="Date" Type="String" />

    <asp:Parameter Name="Name" Type="String" />

    <asp:Parameter Name="Email" Type="String" />

    <asp:Parameter Name="Organization" Type="String" />

    <asp:Parameter Name="Tribute" Type="String" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="Date" Type="String" DefaultValue="DateTime.Now.ToString()" />

    <asp:Parameter Name="Name" Type="String" />

    <asp:Parameter Name="Email" Type="String" />

    <asp:Parameter Name="Organization" Type="String" />

    <asp:Parameter Name="Tribute" Type="String" />

    </InsertParameters>

    </asp:AccessDataSource>

    Friday, May 22, 2009 12:17 AM

Answers

  • User-821857111 posted

    If you are inserting the current date, you should use the built-in Access function DATE() in your SQL:

    InsertCommand="INSERT INTO [Tributes] ([Date], [Name], [Email], [Organization], [Tribute]) VALUES (DATE(), ?, ?, ?, ?)"

    MS Access Date And Time With ASP.NET : http://www.mikesdotnetting.com/Article.aspx?ArticleID=92

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2009 5:18 AM

All replies

  • User-1313886874 posted

     hi rick..

    try this, in code behind.

    AccessDataSource1.InsertParameters["Date"].Default value=DateTime.Now.ToString();
    AccessDataSource1.Insert();

    Friday, May 22, 2009 1:46 AM
  • User-903370090 posted

    Shouldn't the "Date" parameter be of type DateTime and not String?

    Friday, May 22, 2009 1:58 AM
  • User-821857111 posted

    If you are inserting the current date, you should use the built-in Access function DATE() in your SQL:

    InsertCommand="INSERT INTO [Tributes] ([Date], [Name], [Email], [Organization], [Tribute]) VALUES (DATE(), ?, ?, ?, ?)"

    MS Access Date And Time With ASP.NET : http://www.mikesdotnetting.com/Article.aspx?ArticleID=92

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2009 5:18 AM
  • User-1313886874 posted

    hi rick....

    Date() function in the sql query will give the current date value....

    If you are inserting the current date, you should use the built-in Access function DATE() in your SQL:

    InsertCommand="INSERT INTO [Tributes] ([Date], [Name], Email, [Organization], [Tribute]) VALUES (DATE(), ?, ?, ?, ?)"

    MS Access Date And Time With ASP.NET : http://www.mikesdotnetting.com/Article.aspx?ArticleID=92

     


    You are right Mr.Mike 

     

    INSERT INTO TEST (MyDate) VALUES (Date()) 
     
    Friday, May 22, 2009 5:56 AM
  • User446063609 posted

    Awesome!  The Date() is the one.  Thanks allot everyone for your time in responding to this!

    Friday, May 22, 2009 9:07 AM