?
Solved

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

Posted on 2014-08-06
3
Medium Priority
?
217 Views
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();

                try
                {
                    // Open Connection
                    conn.Open();

                    // 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;
                    command.ExecuteNonQuery();
               }
                catch
                {
                    // Display error
                    lblMessage.Text = e.ToString();
                    lblMessage.Visible = true;
                }
                finally
                {
                    // Close Connection
                    conn.Close();
                    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

Bob
0
Comment
Question by:Bob Bender
3 Comments
 
LVL 36

Accepted Solution

by:
ste5an earned 1000 total points
ID: 40244093
Ahm, yes. It's DateTime.Now instead of DateTime.Today.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 1000 total points
ID: 40244097
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

0
 
LVL 5

Author Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

839 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