title: Increment DateTime value based on total number of days
body: <p>When a user adds a class they choose the start date and the end date of the class. I have been able to calculate the value between the end date and start date. I was able to create a For loop to insert a row into the Class table for each individual day the class is on. E.g 01/03/17 - 05/03/17 runs for 5 days so it adds in 5 total rows to the Class table. </p>
  
  <p>Currently for each row it adds in the start date as 01/03/2017 00:00:00 and end date 05/03/2017 00:00:00. </p>
  
  <p>For each row I want to increment the date by 1 until the end date has been reached</p>
  
  <p>For example:   </p>
  
  <pre><code>01/03/2017 00:00:00  
  02/03/2017 00:00:00   
  03/03/2017 00:00:00   
  04/03/2017 00:00:00   
  05/03/2017 00:00:00
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/M5e6n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M5e6n.png" alt="enter image description here"></a>  </p>
  
  <p>Below is my code for my For loop:</p>
  
  <pre><code>  public class UpdateTimetable
    {
      internal void insert_days(int moduledata, int? recurrencedata, DateTime startdatedata, DateTime enddatedata, int classtypedata, int roomcodedata, int starttimedata, int endtimedata, string totalday)
      {
          int tdays = Convert.ToInt16(totalday);
          for (int i = 1; i &lt;= tdays; i++)
          {
              string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  
              SqlConnection myConnection = new SqlConnection(connectionString);
  
              myConnection.Open();
  
              string query = "INSERT INTO Class (ModuleId, ClassTypeId,  ClassScheduleStartTimeId, ClassScheduleEndTimeId, RoomCodeId, StartTime, EndTime, RecurrenceId) VALUES (@moduledata, @classtypedata, @starttimedata, @endtimedata, @roomcodedata, @startdatedata, @enddatedata,  @recurrencedata)";
              SqlCommand myCommand = new SqlCommand(query, myConnection);
  
              //myCommand.Parameters.AddWithValue("@daydata", DayId);
              myCommand.Parameters.AddWithValue("@moduledata", moduledata);
              myCommand.Parameters.AddWithValue("@classtypedata", classtypedata);
              myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
              myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
              myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
              myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
              myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
              myCommand.Parameters.AddWithValue("@recurrencedata", recurrencedata);
  
              myCommand.ExecuteReader();
              myConnection.Close();
          }
      }
  
  }
  </code></pre>
  
  <p>This is my code for calculating the total number of days between start and end date. </p>
  
  <pre><code>            int moduledata = Convert.ToInt32(ddlModule.Text);
              DateTime startdatedata = Convert.ToDateTime(txtstartdate.Text);
              DateTime enddatedata = Convert.ToDateTime(txtenddate.Text);
              int classtypedata = Convert.ToInt32(ddlClassType.Text);
              int roomcodedata = Convert.ToInt32(ddlRoomCode.Text);
              int starttimedata = Convert.ToInt32(ddlStartClassTime.Text);
              int endtimedata = Convert.ToInt32(ddlEndClassTime.Text);
  
              startdatedata = DateTime.Parse(txtstartdate.Text).Date;
              enddatedata = DateTime.Parse(txtenddate.Text).Date;
  
              TimeSpan totaldays = enddatedata - startdatedata;
  
              // This is rounding the TimeSpan to the day value only
              string totalday = ((int)Math.Round(totaldays.TotalDays, MidpointRounding.AwayFromZero)).ToString();
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OOPiB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OOPiB.png" alt="enter image description here"></a></p>
  
  <pre><code>public class UpdateTimetable
  {
   internal void insert_days(int moduledata, int? recurrencedata, DateTime startdatedata, DateTime enddatedata, int classtypedata, int roomcodedata, int starttimedata, int endtimedata, string totalday)
      {
          int tdays = Convert.ToInt16(totalday);
  
          DateTime startDate = DateTime.Now.AddDays(tdays);
          DateTime endDate = enddatedata;
  
          List&lt;string&gt; Dates = new List&lt;string&gt;();
  
          for (int i = 1; i &lt;= endDate.Subtract(startDate).Days; i++)
          {
              Dates.Add(startDate.AddDays(i).ToString());
              string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  
              SqlConnection myConnection = new SqlConnection(connectionString);
  
              myConnection.Open();
  
              string query = "INSERT INTO Class (ModuleId, ClassTypeId,  ClassScheduleStartTimeId, ClassScheduleEndTimeId, RoomCodeId, StartTime, EndTime, RecurrenceId) VALUES (@moduledata, @classtypedata, @starttimedata, @endtimedata, @roomcodedata, @startdatedata, @enddatedata,  @recurrencedata)";
              SqlCommand myCommand = new SqlCommand(query, myConnection);
  
              //myCommand.Parameters.AddWithValue("@daydata", DayId);
              myCommand.Parameters.AddWithValue("@moduledata", moduledata);
              myCommand.Parameters.AddWithValue("@classtypedata", classtypedata);
              myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
              myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
              myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
              myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
              myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
              myCommand.Parameters.AddWithValue("@recurrencedata", recurrencedata);
  
              myCommand.ExecuteReader();
              myConnection.Close();
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p><a href="https://i.stack.imgur.com/E7Dnf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E7Dnf.png" alt="enter image description here"></a></p>