List
string emailPermissionsTable = "EmailPermissions";
sqlConn.ConnectionString = sqlConnStr;
sqlConn.Open();
sqlComm.Connection = sqlConn;
sqlComm.CommandText = "SELECT * FROM " + emailPermissionsTable + " WHERE Username = '" + user + "';";
sqlDr = sqlComm.ExecuteReader();
emailChoices.Clear();
while (sqlDr.Read())
{
string emailAddy = sqlDr["EmailRecipient"].ToString();
emailChoices.Add(emailAddy);
}
emailListBox.DataSource = emailChoices;
emailListBox.DataBind();
... and the relevant code-behind:
I can verify that the emailChoices list is being populated correctly. Why aren't the list elements showing up in the listbox? What have I missed? (Even if I look at the page source, it doesn't seem that the list is being loaded.)
Suthish NairPosted Jun 17, 2011, 3:59 AM
Why you need an extra variable..
while (sqlDr.Read())
{
emailListBox.Items.Add(sqlDr["EmailRecipient"].ToString());
}
Matt FunkePosted Jun 17, 2011, 10:03 AM
I ended up removing the extra variable, thanks, exactly the way you suggested. There seems to be a deeper problem that I don't have time to trace going on here... in the meantime, though, I'll just play with ListBox.Items as if it were a list of strings. It seems to be doing the job quite nicely.
Matt FunkePosted Jun 17, 2011, 9:57 AM
No errors. Sorry. And I can put this code into a brand-new project and have it work, so something deeper must be going on.
Sam HobbsPosted Jun 15, 2011, 5:12 PM
Another relatively common beginner mistake is that you might have more than one ListBox and the one that you put data into is not the same one that you see.
Or maybe something is happening to emailChoices. During execution, after the page is built and you see nothing in the ListBox, if you can, look at emailListBox.DataSource to see what it has.
Pradeep ChandrakerPosted Jun 15, 2011, 4:27 PM
emailListBox.Items.Clear();
emailListBox.Items.Add("AAAAA");
emailListBox.Items.Add("BBBBB");
emailListBox.Items.Add("CCCCC");
emailListBox.DataBind();
Matt FunkePosted Jun 15, 2011, 7:34 AM
[EDIT]:
All right... here's something weird.
If I remove the SQL block, ignore the list entirely, and replace it with this:
emailListBox.Items.Clear();
emailListBox.Items.Add("AAAAA");
emailListBox.Items.Add("BBBBB");
emailListBox.Items.Add("CCCCC");
emailListBox.DataBind();
... nothing shows up in the listbox. But if I put a breakpoint after the DataBind() invocation, then use the Visual Studio IDE to investigate the emailListBox object, look at "Non-Public members", then "base", then "items", then "Non-Public members", then "listItems", I can see that the object seems to be populated correctly (the list items are in there). Does this yield any insight?
Pradeep ChandrakerPosted Jun 14, 2011, 8:18 PM
Can you simply try this? just for testing.
Let me know if you still see issue.