title: C# - How To Retrieve SQL Server Time(7) Into TimeSpan
body: <p>I'm trying to understand how to retrieve time data from a SQL Server 2012 table column defined as <code>Time(7)</code>. I've looked everywhere and have struggled with this. Here is some simple code that I'm using to just try to get it to work.  Can you please help me??</p>
  
  <p>When the code below runs (using Visual Studio 2013) I get the error on the <code>TimeSpan DBStartTime...</code> line:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.</p>
  </blockquote>
  
  <p>I have no idea how to fix this.</p>
  
  <pre><code>var cnnString = ConfigurationManager.ConnectionStrings["TaktBoardsConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(cnnString); 
  
  SqlCommand comm = new SqlCommand();
  comm.CommandText = "SELECT * FROM ScheduleDetail WHERE ScheduleID = " + lstShifts.SelectedValue;
  comm.CommandType = CommandType.Text;
  comm.Connection = conn;
  
  SqlDataReader reader;
  
  conn.Open();
  reader = comm.ExecuteReader(); 
  
  while (reader.Read())
  {
      TimeSpan DBStartTime = Convert.ToDateTime(reader["StartTime"]).TimeOfDay;
      TimeSpan DBEndTime = Convert.ToDateTime(reader["EndTime"]).TimeOfDay;
  
      // Add more coding once this works.
  }
  
  conn.Close();
  </code></pre>