Combobox problem

This is a discussion on Combobox problem within the C# Programming forums, part of the General Programming Boards category; Hello, i have a problem when filling a combobox by setting its datasource to a listArray that contains objects. I ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

    Combobox problem

    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
    Attached Images Attached Images  

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Perhaps id has to be a string?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21