title: C# Populate a ComboBox from table in database
body: <p>I have a combobox which is called <code>combobox1</code>, which I want to populate it with <code>id</code> as value and <code>Name</code> as display name. I searched and read some tutorial and found this code to use in Form load event, but it doesn't populate the list. I see an empty dropdown. Any ideas of where I am wrong? </p>
  
  <p>In my database class I have this function.</p>
  
  <pre><code>public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName)
  {
     SqlDataReader dr;
  
     SqlConnection myConnection = new SqlConnection(CONNECTION_STRING);
     try
     {
        myConnection.Open();
     }
     catch (Exception e)
     {
        Console.WriteLine(e.ToString());
     }
  
     // Check whether the Drop Down has existing items. If YES, empty it.
     if (DropDownName.Items.Count &gt; 0)
        DropDownName.Items.Clear();
  
     SqlCommand cmd = new SqlCommand(Query, myConnection);
     dr = cmd.ExecuteReader();
  
     while (dr.Read())
        DropDownName.Items.Add(dr[0].ToString());
  
     Console.Write(DropDownName.Items.Add(dr[0].ToString()));
     dr.Close();
  }
  </code></pre>
  
  <p>In my form i call it as</p>
  
  <pre><code>private void sales_record_Load(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(DBUtils.CONNECTION_STRING);
      DBUtils.FillDropDownList("select id,Name from Farms", comboBox1);
  }
  </code></pre>