question

Julien-2587 avatar image
0 Votes"
Julien-2587 asked alessandrocaliaro commented

Display the ListView

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered alessandrocaliaro commented

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


· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In my case, I have a database with over 12,000 users and this link seemed to explain that it was more suitable in terms of performance.

performance



Otherwise, I changed the Prénom to Prenom, without success.

This is a compilation error because I probably can't call the right objects, but I can't find it.
In my code I read the data from the database but I don't do anything with it - I should assign the data to local variables but I can't seem to use them despite having modeled my data with a Class MesClients.

0 Votes 0 ·

11/12/2017 it's a very old article. Now CollectionView is the way.
BTW, ListView or CollectionView, does not have sense to visualize 12000 articles...

0 Votes 0 ·

Ok but if ListView or CollectionViews are deprecated what should I use, how and with what examples?

0 Votes 0 ·
Show more comments
Julien-2587 avatar image
0 Votes"
Julien-2587 answered alessandrocaliaro commented

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>;
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

"it was recommended by the Microsoft doc to switch to ListView" can you post a link to this doc?

Try to rename "Prénom" to "Prenom"

0 Votes 0 ·