1

So I have this code that fetches my query.

public class Person
{
   public string Ad_Name { get; set; }
}

public string[] getNameAd(string username)
{
   List<String> columnData = new List<String>();
   //var AdNameList = new List<Person>();
   this.OpenConnection();
   string query = "SELECT `Ad_Name` FROM `cpcboos1_textinfo`.`Ad_Info` WHERE     `Ad_Info`.`User` = '" + username + "' LIMIT 0, 1000";
   MySqlCommand cmd = new MySqlCommand(query, connection);
   using (var reader = cmd.ExecuteReader())
   {
      while (reader.Read())
      {
         columnData.Add(reader.GetString(0));
      }
   }
   return columnData.ToList<string>().ToArray();
}

I reference this code in my Load function on my Main Form.

DBConnect sql = new DBConnect(" *snip* ");
stat_campaign.Items.AddRange(sql.getNameAd(tbx_lgn_user.ToString().Trim()));

However all this returns is the text: "(Collection)". That's it. I ran the query on the SQL databse and it returns the correct results. (Just two of them for now.) What am I doing wrong?

2 Answers 2

4

You have a couple things going on here, but here are my suggestions :

  • Pick one enumerable type to work with and go with it. In this case, you really don't have to switch between an array and a List<> - I would have your getNameAd method return a List<string> so you can bind it to your control. This would also help you organize your code a bit (see below).

  • Having said that, why not just set your string list as the .DataSource of the listbox control?

    // get the username    
    var userName = tbx_lgn_user.ToString().Trim();
    
    // get the query results 
    var listofStrings = sql.getNameAd(userName);
    
    // bind the list to your ListBox control
    stat_campaign.DataSource = listofStrings;
    
Sign up to request clarification or add additional context in comments.

6 Comments

I'll try this and let you know how it works out. EDIT: This works when the listbox is updated while the program is running. However I need it to fetch the contest of the list box from my server when the Form loads.
Are you sure you've added this to the {YourForm}_Load(object sender, EventArgs e) event? Also, make sure you have leave the DataSource property of the combo box control set to (none). One other thing possibly, are you sure you're method is returning you a populated list of strings when the form loads?
It's all set to none. Now it returns a value however it's just the following: "(Collection)". No matter what I do I can't seem to solve this. Do you have a Team Viewer that you could help me with?
I suppose I could try, although I still feel like there's something obvious that's causing your combo-box to return "(Collection)". Are you sure you don't have that listed under the "Items" property? I mean the actual string "(Collection)".
I don't have anything that should be returning that. Do you have a Skype or something that I can chat with you on?
|
0

I'm not sure but try removeing the "ToList()" in your return statement. columnData is already a List type. Try changing your return statement to:

return columnData.ToArray<string>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.