Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello,

I am trying to get some data from a class that I retrievied from my database into a listbox in the form. I have no idea how to do this. I tried searching google and other sites without succes. Below is my code.
// Class: BugFinder
public void GetBugs()
        {
            MySqlDataReader reader = Database.Reader("select * from bugs");
            while (reader.Read())
            {
                Bug bug = new Bug();
                bug.id = reader.GetInt32("id");
                bug.name = reader.GetString("name");
                bug.project = reader.GetString("project");
                bug.category = reader.GetString("category");
                bug.viewStatus = reader.GetString("viewstatus");
                bug.reporter = reader.GetString("reporter");
                bug.assignedTo = reader.GetString("assignedto");
                bug.priority = reader.GetString("priority");
                bug.severity = reader.GetString("severity");
                bug.reproducibillity = reader.GetString("reproducibillity");
                bug.status = reader.GetString("status");
                bug.platform = reader.GetString("platform");
                bug.os = reader.GetString("os");
                bug.osVersion = reader.GetString("osversion");
                bug.summary = reader.GetString("summary");
                bug.description = reader.GetString("description");
                bug.reproduceSteps = reader.GetString("reproducesteps");
                bug.addInfo = reader.GetString("addinfo");
                bug.dateSub = reader.GetDateTime("datesub");
                bug.lastUpdate = reader.GetDateTime("lastupdate");
            }
        }

// Class: Bug
       public int id;
       public string name;
       public string project;
       public string category;
       public string viewStatus;
       public string reporter;
       public string assignedTo;
       public string priority;
       public string severity;
       public string reproducibillity;
       public string status;
       public string platform;
       public string os;
       public string osVersion;
       public string summary;
       public string description;
       public string reproduceSteps;
       public string addInfo;

       public DateTime dateSub;
       public DateTime lastUpdate;

// Main form:
// Listbox name: bugsBox/


I have no idea how to get the bugs into a listbox. ( I know how to populate a listbox, listview. But not in this way.)

Thanks in advance.
Posted

1 solution

The first thing you need is to keep a list or similar of the bugs you are creating as you read them from the DB. Otherwise you cannot add them to anything!

I would suggest at class level you use a List<T>:

// Class: BugFinder

private List<Bug> myBugs;

public void GetBugs()
        {
            MySqlDataReader reader = Database.Reader("select * from bugs");
            myBugs = new List<Bug>();
            while (reader.Read())
            {
                Bug bug = new Bug();
                bug.id = reader.GetInt32("id");
                ...
                bug.lastUpdate = reader.GetDateTime("lastupdate");
                myBugs.Add(bug);
            }
        }
What you have to do to add the bugs to a List box depends on what you want to display in the list box. You say you know how to populate a ListBox - what is stopping you from adding your Bug information?



Ok, but now I can just add the list to my listbox/listview. Like this:
BugFinder finder = new BugFinder();
finder.GetBugs();
recentlyBox.Items.Add(""); (What should I put here?)


It depends on what you think you need to display. I would be tempted to go for a couple of small changes:
1) Make your GetBugs method return the list of bugs:
public List<Bugs> GetBugs()
        {
            MySqlDataReader reader = Database.Reader("select * from bugs");
            myBugs = new List<Bug>();
            while (reader.Read())
            {
                Bug bug = new Bug();
                bug.id = reader.GetInt32("id");
                ...
                bug.lastUpdate = reader.GetDateTime("lastupdate");
                myBugs.Add(bug);
            }
        return myBugs;
        }

You can then use them directly.
2) Make Bugs return something the user might want to read as the ToString method:
public override string ToString()
   {
   return name; // just an idea, you may want more here!
   }

You can then add your bugs:
BugFinder finder = new BugFinder();
foreach (Bug bug in finder.GetBugs())
   {
   recentlyBox.Items.Add(bug.ToString());
   }
 
Share this answer
 
v2
Comments
kZR 22-May-11 10:50am    
Didn't know how that I had to put the bugs into a list. Thanks! Gonna try it right away!
OriginalGriff 22-May-11 10:52am    
You don't have to, but if you don't then you can't refer to them later: as soon as you exit your while loop they are available to the Garbage collector for disposal as their are no references to them.
kZR 22-May-11 11:06am    
Ok, but now I can just add the list to my listbox/listview. Like this:
BugFinder finder = new BugFinder();
finder.GetBugs();
recentlyBox.Items.Add(""); (What should I put here?)
OriginalGriff 22-May-11 11:21am    
Answer updated
kZR 22-May-11 11:29am    
Thanks! Great answer and explanetion! It works and it is awesome. Thanks for your excelent help! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900