如何添加数据表中的记录

发布于 2025-02-11 11:48:25 字数 2239 浏览 1 评论 0原文

我会在表格中添加记录以填写表格,但是listerpf()有错误

“对象引用未设置为对象的实例”

这是整个过程:

await App.methode.AddNewPF(Label_NumeroNF.Text, DatePicker_Date.Date, Editor_LibelleNF.Text, Picker_TypeFrais.SelectedItem.ToString(), double.Parse(Entry_Quantite.Text), double.Parse(Entry_Tarif.Text), double.Parse(Entry_Montant.Text), double.Parse(Entry_MontantTotal.Text), CheckBox_CCEntreprise.IsChecked,  int.Parse(Entry_Imputation.Text));
Navigation.PushAsync(new PostesNF());

“ AddNewpf”背后的代码是:

public async Task AddNewPF(string numero , DateTime date, string libelle, string typeFrais, double quantite, double tarif, double montant, double montantTotal, bool carteCredit,   int imputationCC)
        {
            int result = 0;
            try
            {
                result = await connection.InsertAsync(new DB_PosteNF { Numero = numero, Date = date, Libelle = libelle, TypeFrais = typeFrais, Quantite = quantite, Tarif = tarif, Montant = montant,  MontantTotal = montantTotal, CarteCredit = carteCredit,   ImputationCC = imputationCC});
                StatutMessage = $"{result} poste de frais ajouté : {numero} | {typeFrais} ";
            }
            catch (Exception ex)
            {
                StatutMessage = $"Impossible d'ajouter le poste de frais avec le numéro: {numero}. \nErreur : {ex.Message}";
            }
        }

当出现“ PostEnf”页面时,我有此代码,

protected override async void OnAppearing()
{
    base.OnAppearing();
    CollectionViewPF.ItemsSource = await App.methode.ListerPF();
}

有问题的代码“ listerpf”是:

public async Task<List<DB_PosteNF>> ListerPF(DB_ListeNF datareceived)
{
    try
    {
        string numero = datareceived.Numero;
        //return await connection.Table<DB_PosteNF>().ToListAsync();
        return await connection.Table<DB_PosteNF>().Where(x => x.Numero == numero).ToListAsync();
    }
    catch (Exception ex)
    {
        StatutMessage = $"Impossible d'afficher la liste des postes de frais. \nErreur : {ex.Message}";
    }
    return new List<DB_PosteNF>();
}

但是当我返回到“ postESNF”页面,未显示注册数据。感谢您的帮助 !

I would add record to my table with a form to fill out, but there is a error from ListerPF()

"Object reference not set to an instance of an object"

Here is the whole process:

await App.methode.AddNewPF(Label_NumeroNF.Text, DatePicker_Date.Date, Editor_LibelleNF.Text, Picker_TypeFrais.SelectedItem.ToString(), double.Parse(Entry_Quantite.Text), double.Parse(Entry_Tarif.Text), double.Parse(Entry_Montant.Text), double.Parse(Entry_MontantTotal.Text), CheckBox_CCEntreprise.IsChecked,  int.Parse(Entry_Imputation.Text));
Navigation.PushAsync(new PostesNF());

And the code behind "AddNewPF" is:

public async Task AddNewPF(string numero , DateTime date, string libelle, string typeFrais, double quantite, double tarif, double montant, double montantTotal, bool carteCredit,   int imputationCC)
        {
            int result = 0;
            try
            {
                result = await connection.InsertAsync(new DB_PosteNF { Numero = numero, Date = date, Libelle = libelle, TypeFrais = typeFrais, Quantite = quantite, Tarif = tarif, Montant = montant,  MontantTotal = montantTotal, CarteCredit = carteCredit,   ImputationCC = imputationCC});
                StatutMessage = 
quot;{result} poste de frais ajouté : {numero} | {typeFrais} ";
            }
            catch (Exception ex)
            {
                StatutMessage = 
quot;Impossible d'ajouter le poste de frais avec le numéro: {numero}. \nErreur : {ex.Message}";
            }
        }

When the "PosteNF" page appears, I have this code

protected override async void OnAppearing()
{
    base.OnAppearing();
    CollectionViewPF.ItemsSource = await App.methode.ListerPF();
}

the problematic code "ListerPF" is:

public async Task<List<DB_PosteNF>> ListerPF(DB_ListeNF datareceived)
{
    try
    {
        string numero = datareceived.Numero;
        //return await connection.Table<DB_PosteNF>().ToListAsync();
        return await connection.Table<DB_PosteNF>().Where(x => x.Numero == numero).ToListAsync();
    }
    catch (Exception ex)
    {
        StatutMessage = 
quot;Impossible d'afficher la liste des postes de frais. \nErreur : {ex.Message}";
    }
    return new List<DB_PosteNF>();
}

But when I go back to the "PostesNF" page, the registered data is not displayed. Thanks for your help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞳孔里扚悲伤 2025-02-18 11:48:25

根据您的代码段,该模型是db_postenf带有10个属性。关键是,当您从数据表中检索记录时,我认为您需要一个observableCollection&lt; db_postenf&gt;要接收从数据库读取的这些实体。BELOW是您的参考的代码段:

first ,使用observableCollection

public ObservableCollection<User> UserCollection { get; set; }

然后,然后在on papperatear方法:

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            UserDB db = await UserDB.Instance;
            List<User> a = await db.GetUserAsync();
            UserCollection = new ObservableCollection<User>(a);
            userinfodata.ItemsSource = UserCollection;
        }

ps:在我的情况下,模式为用户

Per your code snippets, the model is DB_PosteNF with 10 properties.The key is that when you retrieve the records from the data table, I think you need a ObservableCollection<DB_PosteNF> to receive these entities that read from the database.Below is the code snippets for your reference:

First, define a UserCollection using ObservableCollection

public ObservableCollection<User> UserCollection { get; set; }

Then, in OnAppearing method:

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            UserDB db = await UserDB.Instance;
            List<User> a = await db.GetUserAsync();
            UserCollection = new ObservableCollection<User>(a);
            userinfodata.ItemsSource = UserCollection;
        }

PS: In my case, the mode is User.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文