I am trying to insert "Date" and "Time" in database table (both are separate columns) using below code:
string query = "Insert into tempTable(List_Id, Date, Time) VALUES('" + lblId.Text + "', @Date, @Time)";
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open();
TimeSpan time = new TimeSpan();
time.ToString();
cmd.Parameters.AddWithValue("@Date", Convert.ToDateTime(DateTime.Now.ToShortDateString()));
cmd.Parameters.AddWithValue("@Time", Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss")));
cmd.ExecuteNonQuery();
connection.Close();
above record is inserted in DB like below:
In Date column: 12/29/2014 12:00:00 AM
In Time column:12/29/2014 5:30:46 PM
Datatype for "Date" and "Time" column is "datetime"
I want that: as I save the Date and Time in DB table, it should display same date and same time what is saved in DB table not current date and time.
I tried below code to fetch saved records from table, after inserting Date and Time in DB:
string query2 = "Select Date, Time where Id='" + itemId + "'";
SqlCommand cmd2 = new SqlCommand(query2, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(query2, connection);
DataTable dt = new DataTable();
da.Fill(dt);
lblDate.Text = DateTime.Parse(dt.Rows[0][0].ToString()).ToString("dd-MM-yyyy");
lblTime.Text = DateTime.Parse(dt.Rows[0][1].ToString()).ToString("HH:MM tt");
but currently it is giving me below output:
Date:29-12-2014
Time:18:04 PM (current time)
Please reply why its showing current date time in place of saved record ??