title: How to populate listbox dynamically with SQL values
body: <p>I am a C# beginner. What I am trying to do is pull data from a column in a SQL database and write it to a listbox. Basically, I want the data in the part_num column of my table to be displayed dynamically in the listbox.</p>
  
  <p>I have seen:</p>
  
  <pre><code>this.listParts.Items.AddRange(new object[] {"Part1", "Part2"});
  </code></pre>
  
  <p>But how would I go about replacing “Part1” and “Part2” with dynamically generated values from SQL?</p>
  
  <pre><code>public mainForm()
  {
      InitializeComponent();
      SqlConnection conn = new SqlConnection(
          "Data Source=DBELL;Initial Catalog=part_table;Integrated Security=True");
      conn.Open();
      DataSet ds = new DataSet();
      SqlDataAdapter adapter = new SqlDataAdapter(
          "SELECT part_num from customParts", conn);
           adapter.Fill(ds);
      foreach (DataRow row in ds.Tables[0].Rows)
      {
          for (int i = 0; i &lt; ds.Tables[0].Columns.Count; i++)
  
          this.listParts.Items.AddRange(new object[] {"Part1", "Part2"});
      }
  }
  </code></pre>
  
  <p>Any help is appreciated!</p>