SQL DateTime and DSropDown Index Question to get both values...

Posted on 2014-08-06
Last Modified: 2014-08-08
Seems simple enuff.  Fill out the form, populate the record okay, EXCEPT that I am not getting the time on the DateTime  field in my SQL table.  Date is fun but Time is Zeroes. (according to the Management Studio tool)

        protected void btnSubmit_OnClick(object sender, EventArgs e)
            if (Page.IsValid)
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
                // Create Command object
                SqlCommand command = conn.CreateCommand();

                    // Open Connection

                    // Create INSERT statement with named parameters
                    command.CommandText = "INSERT INTO ContactUs (ContactUs_ID, ContactUs_first_name, ContactUs_last_name, ContactUs_date_entered, ) VALUES (@ContactUs_ID, @ContactUs_first_name, @ContactUs_last_name, @ContactUs_date_entered)";
                    command.CommandTimeout = 15;
                    command.CommandType = CommandType.Text;

                    // Add Parameters to Command Parameters collection
                    command.Parameters.Add("@ContactUs_ID", SqlDbType.VarChar, 6);
                    command.Parameters.Add("@ContactUs_first_name", SqlDbType.VarChar, 30);
                    command.Parameters.Add("@ContactUs_last_name", SqlDbType.VarChar, 30);
                    command.Parameters.Add("@ContactUs_date_entered", SqlDbType.DateTime2);

                    // Fill Parameters from the controls and get the date
                    command.Parameters["@ContactUs_ATTUID"].Value = userID.Text;
                    command.Parameters["@ContactUs_first_name"].Value = userFirstName.Text;
                    command.Parameters["@ContactUs_last_name"].Value = userLastName.Text;
                    command.Parameters["@ContactUs_date_entered"].Value = DateTime.Today;
                    // Display error
                    lblMessage.Text = e.ToString();
                    lblMessage.Visible = true;
                    // Close Connection
                    lblMessage.Text = "Your message has been saved";
                    lblMessage.Visible = true;

Open in new window

Oh, tried this also....

                    DateTime date1 = new DateTime();
                    date1 = DateTime.Today;
                    command.Parameters["@ContactUs_date_entered"].Value = date1;

any HEPL would be appreciated!!   :D

Question by:Bob Bender
    LVL 32

    Accepted Solution

    Ahm, yes. It's DateTime.Now instead of DateTime.Today.
    LVL 23

    Assisted Solution

    You could just use GETDATE() in your query instead (also, you have a trailing comma in your field list before the close paren before "VALUES", I've removed it here):

    command.CommandText = "INSERT INTO ContactUs (ContactUs_ID, ContactUs_first_name, ContactUs_last_name, ContactUs_date_entered) VALUES (@ContactUs_ID, @ContactUs_first_name, @ContactUs_last_name, GETDATE())";

    Open in new window

    LVL 5

    Author Comment

    by:Bob Bender
    Well, it HAS been a long time away from all those options.  So simple, that a old time gets lost.   Testing now....

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now