Unable to Save Image Data to SQLite Database in MAUI Application

  1. Problem Overview

    What you are trying to do:
    I am trying to save image binary data into a SQLite database named gazouByte.db3 when the “Save” button is pressed after selecting an image file using a file picker. Additionally, I want to display the list of saved images, including their “Id,” “File Name,” and “Extension,” in a CollectionView on the MainPage when the “Get All GazouList” button is pressed.
    What issue you are facing:
    The image binary data is not being saved to the gazouByte.db3 database. When clicking the “Get All GazouList” button, only “got all file list” is displayed, and the “Id,” “File Name,” and “Extension” are not shown. Note that the table gazouByte has been confirmed to be created using “DB Browser for SQLite.”
    [![There is no data in the browser.][1]][1]

  2. Steps to Reproduce

    Specific steps to reproduce the issue:
    Start debugging in Visual Studio 2022 on a Windows machine.
    Click on “Select File” to open the file picker and choose an image file (jpg/png).
    Verify that the image preview is displayed under “Select File.”
    Click the “Save File” button and confirm that “file saved” is displayed.
    Check the database using “DB Browser for SQLite”; the table is created, but no data is saved.
    enter image description here

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using WorkReview.Models;
using System.Collections.Generic;
using Microsoft.Maui.Controls;
using WorkReview.ViewModels;
using System.Runtime.CompilerServices;
namespace WorkReview.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
public void OnGetAllGazouClicked(object sender, EventArgs args)
{
List<GazouByte> gazouByte = App.GazouByteRepo.GetAllGazouBytes();
statusMessage.Text = "got all file list";
}
public void OnFileSaveClicked(object sender, EventArgs args)
{
(BindingContext as MainViewModel).SaveGazouToDataBase(); //BindingVontextのインスタンスをもちいて.Save~メソッドを実行している
statusMessage.Text = "file saved";
}
public async void OnFileSelectClicked(object sender, EventArgs args)
{
try
{
var result = await FilePicker.PickAsync();
var fileExtension = result.ContentType;
var fileName = result.FileName;
if (result != null)
{
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
using (var stream = await result.OpenReadAsync())
{
using (var memoryStream = new MemoryStream())///using(var)はUsingステートメント,{}内の処理が終わったときにリソースが開放される
{
await stream.CopyToAsync(memoryStream); //memoryStreamに保存されたデータをViewModelの該当箇所へ送る / send data to viewmodel
(BindingContext as MainViewModel).gazouExtension = fileExtension;
(BindingContext as MainViewModel).gazouName = fileName;
(BindingContext as MainViewModel).gazouBinary = memoryStream.ToArray();
var previewStream = new MemoryStream(memoryStream.ToArray());
userPreview.Source = ImageSource.FromStream(() => previewStream);
}
}
}
else
{
statusMessage.Text = "Unsupported file type.";
}
}
}
catch (Exception ex)
{
statusMessage.Text = $"Error selecting file: {ex.Message}";
}
}
}
}
</code>
<code>using WorkReview.Models; using System.Collections.Generic; using Microsoft.Maui.Controls; using WorkReview.ViewModels; using System.Runtime.CompilerServices; namespace WorkReview.Views { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new MainViewModel(); } public void OnGetAllGazouClicked(object sender, EventArgs args) { List<GazouByte> gazouByte = App.GazouByteRepo.GetAllGazouBytes(); statusMessage.Text = "got all file list"; } public void OnFileSaveClicked(object sender, EventArgs args) { (BindingContext as MainViewModel).SaveGazouToDataBase(); //BindingVontextのインスタンスをもちいて.Save~メソッドを実行している statusMessage.Text = "file saved"; } public async void OnFileSelectClicked(object sender, EventArgs args) { try { var result = await FilePicker.PickAsync(); var fileExtension = result.ContentType; var fileName = result.FileName; if (result != null) { if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase)) { using (var stream = await result.OpenReadAsync()) { using (var memoryStream = new MemoryStream())///using(var)はUsingステートメント,{}内の処理が終わったときにリソースが開放される { await stream.CopyToAsync(memoryStream); //memoryStreamに保存されたデータをViewModelの該当箇所へ送る / send data to viewmodel (BindingContext as MainViewModel).gazouExtension = fileExtension; (BindingContext as MainViewModel).gazouName = fileName; (BindingContext as MainViewModel).gazouBinary = memoryStream.ToArray(); var previewStream = new MemoryStream(memoryStream.ToArray()); userPreview.Source = ImageSource.FromStream(() => previewStream); } } } else { statusMessage.Text = "Unsupported file type."; } } } catch (Exception ex) { statusMessage.Text = $"Error selecting file: {ex.Message}"; } } } } </code>
using WorkReview.Models;
using System.Collections.Generic;
using Microsoft.Maui.Controls;
using WorkReview.ViewModels;
using System.Runtime.CompilerServices;

namespace WorkReview.Views
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            BindingContext = new MainViewModel();
        }
        

        public void OnGetAllGazouClicked(object sender, EventArgs args)
        {
            List<GazouByte> gazouByte = App.GazouByteRepo.GetAllGazouBytes();
       
            statusMessage.Text = "got all file list";

        }


        public void OnFileSaveClicked(object sender, EventArgs args)
        {

            
            (BindingContext as MainViewModel).SaveGazouToDataBase(); //BindingVontextのインスタンスをもちいて.Save~メソッドを実行している
            statusMessage.Text = "file saved";

        }

        public async void OnFileSelectClicked(object sender, EventArgs args)
        {
            try
            {
                var result = await FilePicker.PickAsync();
                var fileExtension = result.ContentType;
                var fileName = result.FileName;
                if (result != null)
                {
                    if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                        result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                    {
                           
                        using (var stream = await result.OpenReadAsync())
                        {
                            using (var memoryStream = new MemoryStream())///using(var)はUsingステートメント,{}内の処理が終わったときにリソースが開放される
                            {
                                await stream.CopyToAsync(memoryStream); //memoryStreamに保存されたデータをViewModelの該当箇所へ送る / send data to viewmodel
                                (BindingContext as MainViewModel).gazouExtension = fileExtension;
                                (BindingContext as MainViewModel).gazouName = fileName;
                                (BindingContext as MainViewModel).gazouBinary = memoryStream.ToArray();
                                var previewStream = new MemoryStream(memoryStream.ToArray());
                                userPreview.Source = ImageSource.FromStream(() => previewStream);
                            }
                        }
                    }
                    else
                    {
                        statusMessage.Text = "Unsupported file type.";
                    }
                }
            }
            catch (Exception ex)
            {
                statusMessage.Text = $"Error selecting file: {ex.Message}";
            }
        }
    }
}

  1. Expected Behavior

    When starting debugging, a table named gazouByte should be created in the app data.
    After selecting an image using the file picker, the selected image should be previewed under “Select File.”
    When clicking the “Save File” button, the image data should be saved into the gazouByte.db3 database.
    Clicking the “Get All GazouList” button should display the “Id,” “File Name,” and “Extension” of the saved images in the CollectionView.

  2. What I Have Tried

    Verified that exception handling in GazouByteRepositry.AddNewGazouByte method is correctly implemented.
    Confirmed that the table gazouByte is created using “DB Browser for SQLite.”
    Noticed that a System.NullReferenceException occurs when pressing the “Save File” button again without selecting a new image.

  3. Error Messages and Logs

    No error messages are displayed, but the image data is not saved.

  4. Environment

    Windows 11 Home 23H2
    Visual Studio Community 2022 Preview 17.11.0 Preview 4.0
    Microsoft.Maui.Controls 8.0.80
    Microsoft .NET SDK 8.0.107 (x64)
    SQlitePCLRaw.bundle_green 2.1.9
    sqlite-net-pcl 1.9.172
    CommunityToolkit.Mvvm 8.2.2

  5. Additional Information

    The complete code can be found at the following URL:
    https://github.com/rasukuosu/WorkReview.git

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật