Hello, i have a problem when filling a combobox by setting its datasource to a listArray that contains objects.
I make objects from a class like this:
Then I have an other class where i have a function that returns me an arrayList with objects from mssql database.
public int id;
public string firstName;
public string lastName;
public Teacher(int id, string firstName, string lastName)
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
In the main class I tried to populate the combobox like this:
public SqlConnection conn;
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\baza.mdf;User Instance=true";
public ArrayList getAllTeachers()
SqlDataReader reader = null;
SqlCommand command = new SqlCommand("SELECT id, firstName, lastName FROM teachers", conn);
reader = command.ExecuteReader();
ArrayList objArray = new ArrayList();
objArray.Add(new Teacher(Convert.toInt32(reader["id"]), reader["firstName"].ToString(), reader["lastName"].ToString()));
Then i get an error like in the attachment below.
SqlCommands sql = new SqlCommands();
ArrayList list = sql.getAllTeachers();
cmbTeacher.DataSource = list;
cmbTeacher.DisplayMember = "firstName";
cmbTeacher.ValueMember = "id";
Do anyone knows how to solve this?
Thanks in advance
Perhaps id has to be a string?