0

I'm trying to fill a ComboBox with a list. A list that I would previously fill with data from my database using the "SELECT code FROM country;" as a query.

      string MyConString =
         "SERVER=localhost;" +
         "DATABASE=world;" +
         "username=root;" +
         "PASSWORD=1111;";

        string sql = "SELECT code FROM country;";


        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand cmdSel = new MySqlCommand(sql, connection);
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
        da.Fill(dt);

        List<string> data = new List<string>();

How to fill data? I've tried a lot of stuff, it just does not work atm.

viruscro
  • 5
  • 6

1 Answers1

1

Calling AsEnumerable on a DataTable returns an object which implements the generic IEnumerable<T>, so since you have just one column, I think this should work;

List<string> data = dt.AsEnumerable()
                      .Select(r => r.Field<string>("code"))
                      .ToList();
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364