Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > ComboBox and ListBox (obsolete as of Q2 2010) > Help me,get data from database into bombobox

Not answered Help me,get data from database into bombobox

Feed from this thread
  • truong avatar

    Posted on Mar 29, 2011 (permalink)

    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()
            {
                cbClass.FindItemExact("hello");
                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);
                myConnection.Open();
                SqlDataReader myDataReader = myCommand.ExecuteReader();
                Classes cl;
                while (myDataReader.Read())
                {
                    cl = new Classes();
                    cl.Name = myDataReader.GetString(0).ToString();
                    list.Add(cl);
                }
                myDataReader.Close();
                myConnection.Close();
                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

    Reply

  • Ivan Petrov Ivan Petrov admin's avatar

    Posted on Apr 1, 2011 (permalink)

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

    Reply

  • karthik avatar

    Posted on Jun 2, 2011 (permalink)

    how to retrieve data from database into combobox in asp.net

    Reply

  • Cat Cheshire avatar

    Posted on Jun 3, 2011 (permalink)


    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:
    http://www.telerik.com/help/aspnet-ajax/combobox-data-binding-data-source.html

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > ComboBox and ListBox (obsolete as of Q2 2010) > Help me,get data from database into bombobox
Related resources for "Help me,get data from database into bombobox"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]