Easily Load Appointments in WinUI Scheduler with SQLite and Perform CRUD Actions
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (180).NET Core  (28).NET MAUI  (224)Angular  (113)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (41)Black Friday Deal  (1)Blazor  (234)BoldSign  (14)DocIO  (24)Essential JS 2  (110)Essential Studio  (199)File Formats  (72)Flutter  (135)JavaScript  (226)Microsoft  (121)PDF  (83)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (960)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (162)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (10)Business intelligence  (55)Button  (4)C#  (163)Chart  (145)Chart of the week  (57)Cloud  (15)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (73)Development  (676)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (20)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (519)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (45)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (400)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (624)What's new  (337)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Load Appointments in WinUI Scheduler with SQLite and Perform CRUD Actions

Easily Load Appointments in WinUI Scheduler with SQLite and Perform CRUD Actions

In this blog, we’ll see how to load appointments in the WinUI Scheduler control from the SQLite database, execute CRUD (create, read, update, delete) actions, and dynamically update the Scheduler changes.

The Syncfusion WinUI Scheduler has comprehensive scheduling capabilities, making it an excellent solution for appointment management tasks.

SQLite, renowned for its lightweight nature, open-source availability, and self-contained architecture. It is used for relational database management in embedded systems, mobile apps, and desktop software. Its simplicity and efficiency make it a popular choice among developers.

Note: Before proceeding, ensure you have familiarized yourself with the Getting Started with WinUI Scheduler documentation.

Create SQLite connection, define the model, and populate appointments

Let’s start by connecting the SQLite database with the Syncfusion WinUI Scheduler.

Step 1: Install the required package

First, install the sqlite-net-pcl package to connect the SQLite database.

sqlite net pcl NuGet packageStep 2: Create the SQLite connection

Define the SQLite connection using the dynamic SQLiteConnection API and set up the database path property in the SchedulerDatabase.cs file.

Refer to the following code example.

SchedulerDatabase.cs

readonly SQLiteConnection sqLiteConnection;
public SchedulerDatabase(string dbPath)
{
    sqLiteConnection = new SQLiteConnection(dbPath);
}

Create an instance for the SQLite connection with the database Path property and initialize it in the App.Xaml.cs file to use the database.

App.xaml.cs

static SchedulerDatabase database;
public static SchedulerDatabase Database
{
    get
    {
        if (database == null)
        {
            database = new SchedulerDatabase(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WPFSchedulerDatabase.db3"));
        }
        return database;
    }
}

Step 3: Define the database table model

Then, define the Appointment model class to hold the key property values extracted from the database table.

Appointment.cs

public class Appointment
{
    [PrimaryKey, AutoIncrement, Unique]
    public int Id { get; set; }
    public string EventName { get; set; }
    public DateTime From { get; set; }
    public DateTime To { get; set; }
    public bool AllDay { get; set; }
    public string Notes { get; set; }
}

Create a table named Appointment in that SQLite database.

SQLiteDatabase.cs

sqLiteConnection.CreateTable<Appointment>();

Step 4: Populate SQLite database appointments

In the SchedulerViewModel class, populate the data from the SQLite database.

public ObservableCollection<ScheduleAppointment> Appointments { get; set; }
var dataBaseAppointments = App.Database.GetSchedulerAppointments();
if (dataBaseAppointments != null && dataBaseAppointments.Count > 0)
{
    Foreach (Appointment appointment in dataBaseAppointments)
    {
        Appointments.Add(new ScheduleAppointment()
        {
            StartTime = appointment.From,
            EndTime = appointment.To,
            Subject = appointment.EventName,
            IsAllDay = appointment.AllDay,
            Id = appointment.Id
        });
    }
}

 Then, retrieve the appointments from the SQLite database in the SchedulerDatabase.cs file.

public List<Appointment> GetSchedulerAppointments()
{
    return sqLiteConnection.Table<Appointment>().ToList();
}

Step 5: Bind appointments to the Scheduler

Initialize the WinUI Scheduler control and bind its ItemSource property to the Appointments property of the SchedulerViewModel class.

Refer to the following code example.

MainPage.xaml

xmlns:scheduler="using:Syncfusion.UI.Xaml.Scheduler"
<Grid x:Name="grid">
    <Grid.DataContext>
        <local:SchedulerViewModel/>
    </Grid.DataContext>
    <scheduler:SfScheduler x:Name="Scheduler"  ViewType="Week"  AppointmentEdi-torClosing="Scheduler_AppointmentEditorClosing"
                     Al-lowedViewTypes="Day,Week,WorkWeek,Month,TimelineDay,TimelineWeek,TimelineWorkWeek,TimelineMonth"
                     ItemsSource="{Binding Appointments}">
    </scheduler:SfScheduler>
</Grid>

After executing the previous code examples, we’ll get output like in the following image.

Populating appointments from SQLite database in WinUI Scheduler
Populating appointments from SQLite database in WinUI Scheduler

Build a schedule appointment editor

Let’s create an appointment editor that enables the addition, saving, and deletion of appointments in the WinUI Scheduler.

When double-tapping the Scheduler, you will be able utilize the appointment editor to add, edit, and delete appointment details using the Scheduler AppointmentEditorClosing event.

private void Scheduler_AppointmentEditorClosing(object sender, AppointmentEditor-ClosingEventArgs e)
{
    SchedulerViewModel viewModel = this.grid.DataContext as SchedulerViewModel;
    if (viewModel != null)
    {
        if (e.Action == AppointmentEditorAction.Add || e.Action == AppointmentE - ditorAction.Edit)
        {
            viewModel.AddAppointmentDetails(e.Action.ToString(), e.Appointment);
        }
        if (e.Action == AppointmentEditorAction.Delete && e.Appointment != null)
        {
            viewModel.DeleteSchedulerAppointment(e.Appointment);
        }
    }
}

Refer to the following image.

Appointment editor in the WinUI Scheduler
Appointment editor in the WinUI Scheduler

When clicking Save or Delete in the appointment editor, it is necessary to update the WinUI Scheduler and the SQLite database.

Perform CRUD operations with SQLite database and update in the WinUI Scheduler

Let’s see how to execute CRUD actions on the SQLite database and synchronize the changes with the WinUI Scheduler control using the Scheduler appointment editor.

Save an appointment

You can add appointments by providing the required details and clicking the Save button in the appointment editor. The following code example illustrates the command associated with the Save button.

SchedulerViewModel.cs

internal async void AddAppointmentDetails(string action, ScheduleAppoint-ment scheduleAppointment)
{
    appointment = scheduleAppointment;
    var endDate = appointment.ActualEndTime.Date;
    var startDate = appointment.ActualStartTime.Date;
    var endTime = appointment.ActualEndTime.TimeOfDay;
    var startTime = appointment.ActualStartTime.TimeOfDay;
    if (endDate < startDate)
    {
        await ShowDisplayAlert("End date should be greater than start date");
    }
    else if (!appointment.IsAllDay && endDate == startDate)
    {
        if (endTime <= startTime)
        {
            await ShowDisplayAlert("End time should be greater than start time");
        }
        else
        {
            AppointmentDetails(action, appointment);
        }
    }
    else
    {
        AppointmentDetails(action, appointment);
    }
}
private void AppointmentDetails(string action, ScheduleAppointment scheduleAp-pointment)
{
    if (action == "Add" && this.Appointments != null)
    {
        appointment.Id = Appointments.Count;
        //// Add the appointments in the Scheduler.
        Appointments.Add(appointment);
    }
    SaveSchedulerAppointmentAsync();
}

Refer to the following code example to save an appointment in the SQLite database.

SchedulerViewModel.cs

private void SaveSchedulerAppointmentAsync()
{
      //// - add or edit the appointment in the database collection.
      var editAppointment = new Appointment() { From = appointment.StartTime, To = appointment.EndTime, AllDay = appointment.IsAllDay, Notes = appointment.Notes, EventName = appointment.Subject, Id = (int)appointment.Id };
      App.Database.SaveSchedulerAppointment(editAppointment);
}

SchedulerDatabase.cs

//Insert an appointment in the database.
public int SaveSchedulerAppointment(Appointment appointment)
{
    return sqLiteConnection.InsertOrReplace(appointment);
}

Deleting an appointment

By clicking the Delete button, we can delete an appointment in the WinUI Scheduler and the SQLite database. The following code examples demonstrate the command associated with the Delete button.

SchedulerViewModel.cs

internal void DeleteSchedulerAppointment(ScheduleAppointment scheduleAppointment)
{
    appointment = scheduleAppointment;

    //// Remove the appointments from the Scheduler.
    Appointments.Remove(this.appointment);
    //// Delete appointment from the database.
    var deleteAppointment = new Appointment() { From = appointment.StartTime, To = appointment.EndTime, AllDay = appointment.IsAllDay, Notes = appointment.Notes, EventName = appointment.Subject, Id = (int)appointment.Id };
    App.Database.DeleteSchedulerAppointment(deleteAppointment);
}

SchedulerDatabase.cs

public int DeleteSchedulerAppointment(Appointment appointment)
{
    return sqLiteConnection.Delete(appointment);
}
Performing CRUD operations in SQLite database using WinUI Scheduler
Performing CRUD operations in SQLite database using WinUI Scheduler

When rerunning the application, the WinUI Scheduler will fetch the updated appointments from the SQLite database, and the output will be as follows.

Updating the WinUI Scheduler appointments from the SQLite database
Updating the WinUI Scheduler appointments from the SQLite database

GitHub reference

For more details, refer to the Loading Appointments in WinUI Scheduler with SQLite and Perform CRUD operations GitHub demo.

Conclusion

Thanks for reading! In this blog, we learned how to load appointments in the WinUI Scheduler with the SQLite database and perform CRUD operations with ease. Try out the steps discussed in this blog and leave your feedback in the comments section below!

Existing customers can access the new version of Essential Studio from the License and Downloads page. If you’re not a Syncfusion customer, why not explore our features with our 30-day free trial?

For questions, you can contact us through our support forumsupport portal, or feedback portal. We’re here to help you every step of the way!

Related blogs

Tags:

Share this post:

Comments (2)

Great article, thanks! We need more WinUI posts like these and more controls for WinUi3 please!

Hi CJ,

Thank you for your feedback. We plan to publish further blogs in WinUI platform.

Please find the Syncfusion WinUI controls and user guide.

Controls or Products – https://www.syncfusion.com/winui-controls
Blogs – https://www.syncfusion.com/blogs/?s=winui
Knowledge Base – https://support.syncfusion.com/kb/desktop/section/90
User Guide – https://help.syncfusion.com/winui/overview

For any questions, you can contact us through our support forum, support portal, or feedback portal. We are pleased to assist you.

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed