SqlCommand cmd = new SqlCommand("SELECT dateReceived FROM table1", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
object sqlDateTime = dr[0];
DateTime? dt = (sqlDateTime == System.DBNull.Value)
? (DateTime?)null
: Convert.ToDateTime(sqlDateTime);
dtDateReceived.Value = sqlDateTime;
}
}
dr.Close();
cmd.Dispose();
conn.Close();
This is my code for getting the dateReceived in table1 considering that it might have a null value on the column dateReceived.
I would like to display it in a datetimepicker in windows form but I am getting an error :
Cannot implicitly convert type 'object' to System.DateTime
All I wanted is to display the value (if not null) onto the datetimepicker control. If null then nothing will happen.