This is a migrated thread and some comments may be shown as answers.

Help me,get data from database into bombobox

3 Answers 36 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
truong
Top achievements
Rank 1
truong asked on 29 Mar 2011, 12:49 PM
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

3 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 01 Apr 2011, 09:00 AM
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
0
karthik
Top achievements
Rank 1
answered on 02 Jun 2011, 10:03 AM
how to retrieve data from database into combobox in asp.net
0
Cat Cheshire
Top achievements
Rank 1
answered on 03 Jun 2011, 06:56 PM

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

Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
truong
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
karthik
Top achievements
Rank 1
Cat Cheshire
Top achievements
Rank 1
Share this question
or