Help me,get data from database into bombobox

4 posts, 0 answers
  1. truong
    truong avatar
    1 posts
    Member since:
    Mar 2011

    Posted 29 Mar 2011 Link to this post

    Dear ...,
    My problem is:
    In my database, I have "CLASS" table. This table has 3 column: ClassID, ClassName and ManagerID

    Now, I want to show all ClassIDs that have the same ManagerID on Combobox.

    My code:
    public class Classes
            private int id;
            private string name;
            public string Name
                get { return name; }
                set { name = value; }
            public int ID
                get { return id; }
                set { id = value; }
            public override string ToString()
                return id.ToString();
    private void LoadDataTocbClass()
                for (int i = 0; i < PrepareDataForcbClass().Count; i++)
                    cbClass.Items.Add(PrepareDataForcbClass()[i]); // ERROR, WHY?
                cbClass.DisplayMember = ("Name");
    private List<Classes> PrepareDataForcbClass()
                List<Classes> list = new List<Classes>();
                string sql = "select ClassID FROM [AttendanceDB].[dbo].[CLASS]" +
                                "where ManagerID=@ManagerID";
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = sql;
                myCommand.Parameters.AddWithValue("@ManagerID", ManagerID);
                SqlDataReader myDataReader = myCommand.ExecuteReader();
                Classes cl;
                while (myDataReader.Read())
                    cl = new Classes();
                    cl.Name = myDataReader.GetString(0).ToString();
                return list;

    Can you help me write an example or help me fix my code.
    I want to get all ClassIDs that have the same ManagerID from database to Combobox.
    In my code, cbClass is my combobox.

    Thanks and best regards,
    Pham Son Truong
  2. Ivan Petrov
    Ivan Petrov avatar
    701 posts

    Posted 01 Apr 2011 Link to this post

    Hi truong,

    Thank you for writing.

    The RadComboBox control is obsolete. I would strongly recommend updating to the new and much improved RadDropDonwList. I have prepared a small example which demonstrates how to bind it to a List. I have commented out your code as I do not have the same database as you, but you should be able to uncomment and run the code on your machine.

    I hope this will help you. If you have further questions, feel free to ask.

    Best wishes,
    Ivan Petrov
    the Telerik team
  3. DevCraft R3 2016 release webinar banner
  4. karthik
    karthik avatar
    1 posts
    Member since:
    Jun 2011

    Posted 02 Jun 2011 Link to this post

    how to retrieve data from database into combobox in
  5. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 03 Jun 2011 Link to this post

    You can populate the combobox with data from a database in various ways.
    For example you can use a datasource component - as described in this article:

Back to Top