Hi,
This is my code:
SqlCommand getPreviousDateCmd = new SqlCommand("SELECT Top 1 Open_date FROM Counter WHERE (Open_date <= @.todays_date) ORDER BY Open_date DESC", myConnection);
//Get previous date
getPreviousDateCmd.Parameters.AddWithValue("@.todays_date", DateTime.Now.ToString("dd/MM/yyyy"));
DateTime previousDateTime =Convert.ToDateTime(getPreviousDateCmd.ExecuteScalar().ToString());
//previousDate = previousDateTime.ToString("dd/MM/yyyy");
LabelToday.Text = previousDateTime.ToString("dd/MM/yyyy");
//If previous date is same as today's date (Get only date and year)
I get a huge unstoppable(?) error message when it converts STRING to DATETIME.
What I want to do is get DATETIME object and change the format to "dd/MM/yyyy"
Could someone give me some ideas? Thankx!
Assuming your query always returns a row (*no* nulls e.g DBNull, when you would need to deal with that too), why converting it to string in the middle? Or converting at all if you know it is a date
DateTime previousDateTime = (DateTime)(getPreviousDateCmd.ExecuteScalar());
No comments:
Post a Comment