Questions
heshaniheshaniasked 4 months ago
I am trying to populate ComboBox with a Database table, but it does not populate the ComboBox. I can see only an empty dropdown. You can see the code that I used below, give any idea for how to fix this.
public static void FillComboBox(string Query, System.Windows.Forms.ComboBox ComboBoxName)
{
   SqlDataReader dr;
    SqlConnection SqlConn = new SqlConnection(ConnectionString);
   try
   {
      SqlConn.Open();
   }
   catch (Exception e)
   {
      Console.WriteLine(e.ToString());
   }
    if (DropDownName.Items.Count > 0)
      DropDownName.Items.Clear();
    SqlCommand cmd = new SqlCommand(Query, SqlConn);
   dr = cmd.ExecuteReader();
    while (dr.Read())
      ComboBoxName.Items.Add(dr[0].ToString());
    Console.Write(ComboBoxName.Items.Add(dr[0].ToString()));
   dr.Close();
}
1