0

I am getting the sequence numbers of the days of the week from a database and I want to make those days of the week available in the DatePicker and make the rest unavailable. But all days become unavailable, except for the last (in my case, this is the last day of the year). What am I doing wrong? I took the code from the link as a basis: How to black out specific days of the week (datepicker) [VB] (by the way, it works).

using (OracleConnection conn = new OracleConnection(connectionString))
{                
    int year = DateTime.Now.Year;
    var minDate = DateTime.Now;
    var maxDate =new DateTime(year, 12, 31);
    string r;
    string st = comboBox2.SelectedItem.ToString();
    string sql3 = "select distinct id_week from timetable,masters where 
    timetable.id_mas=masters.cod and masters.fio = '" + st + "'";
    using (OracleCommand command = new OracleCommand(sql3, conn))
    {
        OracleDataReader reader3 = command.ExecuteReader();

        while (reader3.Read())
        {
            r = reader3[0].ToString();

            for (var d = minDate; d <= maxDate> d; d = d.AddDays(1))
            {
                if ((int)d.DayOfWeek != Convert.ToInt32(r))
                {
                    dateTimePicker1.BlackoutDates.Add(new CalendarDateRange(d));
                }
            }
        }

        reader3.Close();
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Ana7
  • 13
  • 4

1 Answers1

0

To make this work it is necessary to change some logic.

 int dayOfWeek;
 IList<DayOfWeek> dow = new List<DayOfWeek>();

 using (OracleDataReader reader3 = command.ExecuteReader())
 {
     // Prepare days of week that should be available
     while (reader3.Read())
     {
         var r = reader3[0].ToString();     
         if (Int32.TryParse(r, out dayOfWeek))
         {
             dow.Add((DayOfWeek)dayOfWeek); 
         }
     }
 }
 
 // Now prepare the calender  
 for (var d = minDate; d <= maxDate; d = d.AddDays(1))
 { 
     // If day of week not included to the list then blackout it.                
     if (!dow.Contains(d.DayOfWeek))
     {
         dateTimePicker1.BlackoutDates.Add(new CalendarDateRange(d));                            
     }
 }
Jackdaw
  • 7,626
  • 5
  • 15
  • 33