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:
Code:
class Teacher
{
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;
}
}
Then I have an other class where i have a function that returns me an arrayList with objects from mssql database.
Code:
class SqlCommands
{
public SqlConnection conn;
public SqlCommands()
{
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\baza.mdf;User Instance=true";
}
public ArrayList getAllTeachers()
{
conn.Open();
SqlDataReader reader = null;
SqlCommand command = new SqlCommand("SELECT id, firstName, lastName FROM teachers", conn);
reader = command.ExecuteReader();
ArrayList objArray = new ArrayList();
while (reader.Read())
{
objArray.Add(new Teacher(Convert.toInt32(reader["id"]), reader["firstName"].ToString(), reader["lastName"].ToString()));
}
return objArray;
}
}
In the main class I tried to populate the combobox like this:
Code:
SqlCommands sql = new SqlCommands();
ArrayList list = sql.getAllTeachers();
cmbTeacher.DataSource = list;
cmbTeacher.DisplayMember = "firstName";
cmbTeacher.ValueMember = "id";
Then i get an error like in the attachment below.
Do anyone knows how to solve this?
Thanks in advance