Share via

Display the ListView

Julien 156 Reputation points
2021-07-05T07:55:00.267+00:00

Hello, I am new to Xamarin Forms and I have a small project which should allow me to recover data from a MySQL database. I have the connection, but I cannot display the data on my screen. Here are the different codes:

ComptePage.xaml.cs :

using System;
using MySqlConnector;
using TractoApp.Data;
using Xamarin.Forms;
using System.Collections.Generic;

namespace TractoApp.Views
{

    public partial class ComptePage : ContentPage
    {
        public ComptePage()

        private void VoirBdd(object sender, EventArgs e)
        {

            if (Connecté)
            {
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM ps_customer", cn);
                using (MySqlDataReader Lire = cmd.ExecuteReader())
                {                    
                    var data = new List<MesClients>();
                    while (Lire.Read())
                    {
                        string Prénom = Lire["firstname"].ToString();
                        string Nom = Lire["lastname"].ToString();

                        data.Add(new MesClients { firstname = Prénom, lastname = Nom });
                    }
                }
            }
            else
            {
                DisplayAlert("Infos:", "Plantage", "Ok");
            }
        }
    }
}

Mes clients.cs :

using System;
using System.Collections.Generic;
using System.Text;

namespace TractoApp.Data
{
    public class MesClients
    {
        public string firstname;
        public string lastname;

        public MesClients(string firstname, string lastname)
        {
            this.firstname = firstname;
            this.lastname = lastname;
        }
        public string getFirstname()
        {
            return firstname;
        }
        public string getLastname ()
        {
            return lastname;
        }
    }
}

ComptePage.xaml

<ListView x:Name="clientsbdd">
            <ListView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Prénom</x:String>
                    <x:String>Nom</x:String>
                </x:Array>
            </ListView.ItemsSource>
        </ListView>

I have a CS7036 error but I cannot manage my parameters for the compilation.
Thanks in advance.

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

Alessandro Caliaro 4,206 Reputation points
2021-07-05T08:31:17.71+00:00

I don't know about CS7036 but I think you can not the accent here

                         string Prénom = Lire["firstname"].ToString();  

Second, you are creating a Static Listview with two records:
Prenom
Nom

You should take a look to this sample (and use CollectionView instead of ListView)

introduction

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Julien 156 Reputation points
    2021-07-05T13:01:46.567+00:00

    Thank you for the answer. I had already done the tutorial with the CollectionView, it worked fine for a SqlLite database but in my case, it was recommended by the Microsoft doc to switch to ListView. The CS7036, according to StackOverflow is an error due to bad parameters entered on my Class.

    I updated my code on ComptePage.xaml.cs and it gives that but it is to compile the next two lines that Visual Studio does not want.

    private void VoirBdd(object sender, EventArgs e)
            {
    
                if (Connecté)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM ps_customer", cn);
                    using (MySqlDataReader Lire = cmd.ExecuteReader())
                    {                    
                        List<MesClients> clientsbdd = new List<MesClients>();
                        while (Lire.Read())
                        {
                            string Prénom = Lire["firstname"].ToString();
                            string Nom = Lire["lastname"].ToString();
    
                            clientsbdd.Add(new MesClients { firstname = Prénom, lastname = Nom });
                        }
                        clientsbdd.ItemsSource = List<MesClients>;
                    }
                }
                else
                {
                    DisplayAlert("Infos:", "Plantage", "Ok");
                }
            }
    

    The error is on these two lines.

    clientsbdd.Add(new MesClients { firstname = Prénom, lastname = Nom });
    clientsbdd.ItemsSource = List<MesClients>;
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.