Resolved Problem displaying information in a listBox

DavLn

Member
Joined
Mar 31, 2023
Messages
13
Programming Experience
Beginner
Hello, When I display all the agents in my listBox I get the right number of agents but not the right information. I get the result from the attachment.

And here is the code of my function:

C#:
public static List<Utilisateur> GetTousLesAgents()
        {
            List<Utilisateur> ListeUsers = new List<Utilisateur>();
            string query = "SELECT ADRESSEMAIL, MDP FROM utilisateur WHERE TYPE=2";
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = query;
            using (MySqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    //Console.WriteLine(dataReader.GetValue(0) + " || " +
                    //            dataReader.GetValue(1));
                    string adU = rdr.GetString("ADRESSEMAIL");
                    string mdpU = rdr.GetString("MDP");

                    Utilisateur utilisateur = new Utilisateur(adU, mdpU);
                    ListeUsers.Add(utilisateur);
                }
            }

            return ListeUsers;
        }

And here is the code where I call my function:

C#:
private void btnAfficherAgentsAdmin_Click(object sender, EventArgs e)
        {
            Fonctions.Connexion();
            listBAgentsAdmin.Items.Clear();
            listBAgentsAdmin.Items.AddRange(Fonctions.GetTousLesAgents().ToArray());
        }

I would like some help with my problem if possible of course.
 

Attachments

  • capturePro.PNG
    capturePro.PNG
    4.1 KB · Views: 20
You can override ToString in Utilisateur for it to show the text you want.
 
Recall that in C#, the default implementation of a classes ToString() method is to just return the type name.

You'll need:
- to override the ToString() of your Utilisateur class; OR
- change you call to AddRange() to actually pass in the strings that you want to be displayed.

The later is because you need to recall that the WinForms ListBox technically just displays strings (unless you make it an owner draw listbox). So it will call ToString() on the objects you put into it. If you don't like the default ToString() implementation, or you don't want to override ToString(), then you'll need to give the list box what strings you want it to display.
 

Latest posts

Back
Top Bottom