I am trying to populate a listbox with the following code but I get an error saying ListItem could not be found.
Also rathen than creating a new listbox here I want to populate one on my windows forms gui called listbox1 but it says it does not exist in the current context if I change the first line to ListBox lb = listbox1();
ListBox lb = new ListBox();
string connectionString = "your connection string here";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string query = "SELECT column FROM myitemstable";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read()) {
lb.Items.Add(new ListItem((string)reader["column"]));
}
}
}
}
listbox1.Items.Add(new ListItem((string)reader["column"]));. no need to declare anything