Looping through appointments by date range

5 posts, 0 answers
  1. Adrian Barnes
    Adrian Barnes avatar
    18 posts
    Member since:
    Dec 2009

    Posted 28 Feb 2011 Link to this post

    Outside of the scheduler, I'm trying to loop through all events in a given date range.  Due to the complexity of recurrence, I have a hidden instance of the RadScheduler on my form, and I am looping through the appointments and resources examining each appointment to see if it falls within range.  The process is not smooth.  I'm having lots of trouble with recurrence.  For example, if I select one date, I'm getting two copies of the same event...one with, say, an ID of 105, and the other with an ID of 105_0.  If I select a week, I am only seeing the 105_0 appointment.

    I am finding work-arounds for each issue as I encounter one, but do not have faith that there aren't other hidden problems. Is there a better way to loop through by date range?  The code below is just my latest experimentation. I probably need to scrap and start over.

    private void LoopEvents()
    {
        RadListBox rlbResources = PanelBar.Items[0].Items[0].FindControl("rlbResources") as RadListBox;
        DataTable dt;
        string canceled = "Canceled";
     
        string sql = "SELECT * FROM AppointmentResource";
        string errString = "";
        DateTime currdt = Convert.ToDateTime("1/1/1901");
     
        dt = PageData.GetData(sql, ref errString).Tables[0];
        if (rlbResources.Items.Count == 0)
            rlbResources.DataBind();
     
        foreach (Telerik.Web.UI.RadDate rd in RadCalendar1.SelectedDates)
        {
            foreach (Telerik.Web.UI.Appointment a in RadScheduler1.Appointments)
            {
                    foreach (Telerik.Web.UI.RadListBoxItem l in rlbResources.Items)
                    {
                        if (l.Checked & a.Resources.GetResource("Resource", Convert.ToInt32(l.Value)) != null)
                        {
                             
                            //Occurrences of a recurrence master are not generated untill they
                            //are within RadScheduler's visible range. This mandates the occurrences
                            //information to be extracted from the Master's recurrence rule as follows:
                            if ((a.RecurrenceState == RecurrenceState.Master) & RadCalendar1.SelectedDates.Count > 1)
                            {
                                RecurrenceRule parsedRule;
                                RecurrenceRule.TryParse(a.RecurrenceRule.ToString(), out parsedRule);
     
                                //If a recurring appointment does not have specified end time it will have
                                //endless occurrences. In this case you can explicitly put a restriction:
     
                                if (RadCalendar1.SelectedDates.Count > 1)
                                    parsedRule.SetEffectiveRange(RadCalendar1.SelectedDates[0].Date, RadCalendar1.SelectedDates[RadCalendar1.SelectedDates.Count - 1].Date);
                                else
                                    parsedRule.SetEffectiveRange(RadCalendar1.SelectedDate.AddDays(-1), RadCalendar1.SelectedDate.AddDays(1));
                                 
                                foreach (DateTime occurrence in parsedRule.Occurrences)
                                {
                                    if (occurrence.ToShortDateString() == rd.Date.ToShortDateString())
                                    {
                                            //We have a match
                                    }
                                }
                            }
                            else //Get the rest of the appointments    
     
                                //If the SelectedDate of RadScheduler is set so that the visible range encompasses occurences of     
                                //a recurring appointement, these occurences will be generated now so they will be added once again    
                                //to the DataTable. To prevent this, we use the following check:    
                                //if (a.RecurrenceState != RecurrenceState.Occurrence)
                                //{
                                if (WithinDate(a.Start, a.End, rd.Date))
                                {
                                      //We have a match
                                }
                            //}
     
                        }
                    }
            }
        }
     
    }
  2. André
    André avatar
    15 posts
    Member since:
    Feb 2011

    Posted 01 Mar 2011 Link to this post

    You can LINQ them to get the range you want. 

    For example:

    int daysRange;
    switch(RadSchedulerRoteiros.SelectedView)
    {
        case SchedulerViewType.DayView:
            daysRange = 1;
            break;
      
        case SchedulerViewType.WeekView:
            daysRange = 7;
            break;
          
        case SchedulerViewType.MonthView:
            daysRange = 30;
            break;
      
        default:
            break;
    }
      
    foreach (Appointment appointment in RadSchedulerRoteiros.Appointments.Where(A => A.Start >= RadSchedulerRoteiros.SelectedDate && A.End <= RadSchedulerRoteiros.SelectedDate.AddDays(daysRange).AddHours(23).AddMinutes(59)))
    {
        //do stuff
    }

    This is just an example, and is not getting TimelineView and MultiDayView, as I don't know how to get the range for them... 

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Adrian Barnes
    Adrian Barnes avatar
    18 posts
    Member since:
    Dec 2009

    Posted 01 Mar 2011 Link to this post

    Thanks for the idea, André.  I tried it out, but it skips right over recurring events.  For example, it will show regular appointments on 3/7, 3/17, and 3/20, but skip recurring appointments on 3/8 and 3/24.

    Here is how I modified it to fit my situation:

    foreach (Telerik.Web.UI.Appointment appointment in RadScheduler1.Appointments.Where(A => A.Start >= RadCalendar1.SelectedDates[0].Date && A.End <= RadCalendar1.SelectedDates[RadCalendar1.SelectedDates.Count - 1].Date.AddHours(23).AddMinutes(59)))
    {
        lblEvents.Text += appointment.Subject + " (" + appointment.Start.ToShortDateString() + ")<br />";
    }
  5. Adrian Barnes
    Adrian Barnes avatar
    18 posts
    Member since:
    Dec 2009

    Posted 15 Mar 2011 Link to this post

    So is this something I should open a support ticket about?  I really need to get this issue resolved. Thanks!
  6. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 18 Mar 2011 Link to this post

    Hello Adrian,

    Here is a couple more alternatives you can consider:

    <telerik:RadScheduler runat="server" ID="RadScheduler1" Height="0px" Width="0px"
           onappointmentcreated="RadScheduler1_AppointmentCreated" SelectedView="MonthView">
       </telerik:RadScheduler>
    protected void RadScheduler1_AppointmentCreated(object sender, AppointmentCreatedEventArgs e)
       {
           Response.Write(e.Appointment.Start + "</br>");
       }

    Greetings,
    Peter
    the Telerik team
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017