title: Populating listbox on page load
body: <p>Im attempting to populate a listbox from a database on form load, but when i load the form no data appears. The connection to the database is setup in VS 2017 and thats where I got the connection string.  </p>
  
  <pre><code>namespace listbox
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              fillListBox();
          }
  
          void fillListBox()
          {
              SqlConnection con = new SqlConnection();
              con.ConnectionString = "Data Source=computername\\SQLEXPRESS;Initial Catalog=footfall;Integrated Security=True";
              con.Open();
              SqlCommand cmd = new SqlCommand("select category from category",con);
              SqlDataReader reader = cmd.ExecuteReader();
              categoryBox.Items.Clear();
              while (reader.Read())
              {
                  categoryBox.Items.Add(reader.ToString());
              }
              con.Close();
          }
      }
  }
  </code></pre>