Add Resources to Scheduler from Database

I have three resources (employees) that I want to use with my SfScheduler (.NET Maui) control.  

My appointments get save to a SQLite db with no problem, but I am having difficulty getting the resources to work with my scheduler control. 

I have tried converting the following to a db table but it has not been successful. 

// Adding schedule resource in the scheduler resource collection.
var Resources = new ObservableCollection<SchedulerResource>()
{
   new SchedulerResource() { Name = "Sophia", Foreground = Colors.Blue, Background = Colors.Green, Id = "1000" },
   new SchedulerResource() { Name = "Zoey Addison",  Foreground = Colors.Blue, Background = Colors.Green, Id = "1001" },
   new SchedulerResource() { Name = "James William",  Foreground = Colors.Blue, Background = Colors.Green, Id = "1002" },
};

// Adding the scheduler resource collection to the schedule resources of SfSchedule.
this.Scheduler.ResourceView.Resources = Resources;

How do I get the appointments and resources to be filled in using a SQLite db instead of "hard-coded" in the .cs file?  


5 Replies

SS SaiGanesh Sakthivel Syncfusion Team June 23, 2023 02:14 PM UTC

Hi Frederick,


Currently, we are checking the reported query from our end. We will update you on the further details on June 27, 2023. We appreciate the patience until then.


Regards,
SaiGanesh Sakthivel



SS SaiGanesh Sakthivel Syncfusion Team June 27, 2023 01:40 PM UTC

Frederick,


#Regarding Fetch the appointment and resource from the SQlite database

We have prepared the sample using SQLite database from our side. Please refer to the following steps for your reference.


Step 1: Create the Database

public class SchedulerDatabase

{

    readonly SQLiteConnection _database;

 

    public SchedulerDatabase(string dbPath)

    {

        _database = new SQLiteConnection(dbPath);

        _database.CreateTable<Appointment>();

        _database.CreateTable<Employee>();

    }

 

    //Get the list of appointments from the database

    public ObservableCollection<SchedulerAppointment> GetSchedulerAppointment()

    {

        ObservableCollection<SchedulerAppointment> schedulerAppointments = new ObservableCollection<SchedulerAppointment>();

        var appointments = _database.Table<Appointment>().ToList();

 

        foreach (Appointment appointment in appointments)

        {

            schedulerAppointments.Add(new SchedulerAppointment()

            {

                StartTime = appointment.From,

                EndTime = appointment.To,

                Subject = appointment.EventName,

                ResourceIds = new ObservableCollection<object>() { appointment.ResourceIds }

            });

        }

 

        return schedulerAppointments;

    }

 

    //Insert an appointment in the database

    public int SaveSchedulerAppointmentAsync(Appointment appointment)

    {

        if (appointment == null)

        {

            throw new Exception("Null");

        }

 

        return _database.Insert(appointment);

    }

 

    //Delete an appointment in the database

    public int DeleteSchedulerAppointmentAsync(Appointment appointment)

    {

        return _database.Delete(appointment);

    }

 

    //Get the list of appointments from the database

    public ObservableCollection<SchedulerResource> GetSchedulerResource()

    {

        ObservableCollection<SchedulerResource> schedulerResources = new ObservableCollection<SchedulerResource>();

        var resources = _database.Table<Employee>().ToList();

           

        foreach (Employee employee in resources)

        {

            schedulerResources.Add(new SchedulerResource()

            {

                Name = employee.Name,

                Id = employee.ResourceId,

                Background = new SolidColorBrush(Colors.LimeGreen),

            });

        }

 

        return schedulerResources;

    }

 

    //Insert an appointment in the database

    public int SaveSchedulerResourceAsync(Employee employee)

    {

        if (employee == null)

        {

            throw new Exception("Null");

        }

 

        return _database.Insert(employee);

    }

 

    //Delete an appointment in the database

    public int DeleteSchedulerResourceAsync(Employee employee)

    {

        return _database.Delete(employee);

    }

}


Step 2: Initialize the database in the App.cs

public static SchedulerDatabase Database

{

    get

    {

        if (database == null)

        {

            database = new SchedulerDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchedulerDatabase.db3"));

        }

        return database;

    }

}


Step 3: Fetch the data and set to the scheduler AppointmentSource and resources.

public MainPage()

{

            InitializeComponent();

    CreateTabele();

    this.Scheduler.ResourceView.Resources = App.Database.GetSchedulerResource();

    this.Scheduler.AppointmentsSource = App.Database.GetSchedulerAppointment();

}

 

private void CreateTabele()

{

    var appointments = App.Database.GetSchedulerAppointment();

    if (appointments.Count == 0)

    {

        var todoItem = new Appointment() { From = DateTime.Today.AddHours(10), To = DateTime.Today.AddHours(12), AllDay = false, DescriptionNotes = "Meeting", EventName = "Annual", ID = 1, ResourceIds = "1000" };

        App.Database.SaveSchedulerAppointmentAsync(todoItem);

    }

 

    var resource = App.Database.GetSchedulerResource();

    if (resource.Count == 0)

    {

        var todoItem = new Employee() { Name = "Sophia", ResourceId = "1000", };

        App.Database.SaveSchedulerResourceAsync(todoItem);

        var todoItem1 = new Employee() { Name = "Zoey Addison", ResourceId = "1001" };

        App.Database.SaveSchedulerResourceAsync(todoItem1);

        var todoItem2 = new Employee() { Name = "James William", ResourceId = "1002" };

        App.Database.SaveSchedulerResourceAsync(todoItem2);

    }

}


Please refer to the tested sample in the attachment. Please let us know if you have any concerns.


Regards,
SaiGanesh Sakthivel



SS SaiGanesh Sakthivel Syncfusion Team June 27, 2023 01:43 PM UTC

Frederick,


Please refer to the tested sample in the attachment. Please let us know if you have any concerns.


Regards,
SaiGanesh Sakthivel


Attachment: SchedulerMAUI_be2553cf.zip


FS Frederick Switzer June 27, 2023 08:09 PM UTC

SaiGanesh, 


  1. How is a new appointment with a resource save?
  2. I cannot see any appointments displayed with the assigned resource. 
  3. I cannot find the "db3" in the db path  
  4. "C:\\Users\\xxxx\\AppData\\Local\\SchedulerDatabase.db3"

Thanks


SS SaiGanesh Sakthivel Syncfusion Team June 28, 2023 01:29 PM UTC

Hi Frederick,


#Query 1 and Query 2: How is a new appointment with a resource save? and I cannot see any appointments displayed with the assigned resource.

You can add the new appointment and save to the database with the help of the button clicked. Please refer to the following code snippet for your reference.


Code Snippet

private void Button_Clicked(object sender, EventArgs e)

{

    //// - new appointment

    SchedulerAppointment schedulerAppointment = new SchedulerAppointment()

    {

        StartTime = DateTime.Now,

        EndTime = DateTime.Now.AddHours(1),

        Subject = "New Appointment",

        ResourceIds = new ObservableCollection<object>() { "1000" }

    };

 

    (Scheduler.AppointmentsSource as ObservableCollection<SchedulerAppointment>).Add(schedulerAppointment);

 

    //// - save to DB

    Appointment appointment = new Appointment()

    {

        From = schedulerAppointment.StartTime,

        To = schedulerAppointment.EndTime,

        EventName = schedulerAppointment.Subject,

        ResourceIds = schedulerAppointment.ResourceIds[0].ToString()

    };

       

    App.Database.SaveSchedulerAppointmentAsync(appointment);

}


Please refer to the demo sample in the attachment.


#Query 3: I cannot find the "db3" in the db path

We suggest you search the Local folder by searching in PC (C:\Users\xxxx\AppData\Local) and then search the SchedulerDatabase.db3 in the search box. It will help to find the database file location.


Please let us know if you have any concerns.


Regards,
SaiGanesh Sakthivel


Attachment: SchedulerMAUI_e18b8e5.zip

Loader.
Up arrow icon