0

I getting this very frustrating errors while trying to populate a comboBox.

System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource

Here's the code snippet causing it.

main.cmd.Connection = main.con;
    main.cmd.CommandText = "SELECT num_innovation FROM Innovation INNER JOIN Activite ON Innovation.num_activite = Activite.num_activite WHERE Activite.num_activite = " + comboBox_activite.SelectedValue;
    main.con.Open();
    System.Data.SqlClient.SqlDataReader sdReader = main.cmd.ExecuteReader();
    comboBox_innovation.DisplayMember = "num_innovation";
    comboBox_innovation.ValueMember = "num_innovation";
    comboBox_innovation.DataSource = sdReader; //Error here
    sdReader.Close();
    main.con.Close();
Oussama Essamadi
  • 358
  • 8
  • 15
  • You need to read about, understand and start parameterizing your queries before bobby tables comes to visit. http://bobby-tables.com/ What you have is wide open to sql injection. – Sean Lange Dec 18 '17 at 21:35

1 Answers1

0

The error message says you need to convert your sdReader to a list. See below on how to convert data reader to list: How can I easily convert DataReader to List<T>?

Btw you should also parameterize your query to prevent SQL injection attack.

Try use an ORM framework such as EntityFramework or Nhibernate. It may take some time to learn and get started but will make your life a lot easier in the long run. e.g. easy binding, lambda in query

Robert
  • 486
  • 2
  • 15