I need to display the column names from table in the dropdownlist of the application. How to display columns of table from database to application using c#?

    private void Form1_Load(object sender, EventArgs e)
            {
            //SQL Connection String Modify with Your Data Source and DataBase Name
                string con = "Data Source = Flower-PC;Initial Catalog = Schooldata ; Integrated Security = True";
                // Create Connection 
                SqlConnection cn = new SqlConnection(con);              
               DataTable dt = new DataTable();
                cn.Open();//Sql Conneciton Open
                               // Your Sql Command ,Modify it with Your Query
        SqlCommand cmd = new SqlCommand("Select StudentName from Studentinfo", cn);
                SqlDataAdapter data = new SqlDataAdapter(cmd);
                data.Fill(dt);
                comboBox1.DataSource = dt;
                            //Modify This String with Your Column Name
                comboBox1.DisplayMember = "Studentname";
                comboBox1.ValueMember = "Studentname";



            }

Hope This Help You.

As yousaf said.. instead of "Select StudentName from Studentinfo" query,, you can use this query "Select column_Name from information_Schema.columns where name = '<tablename>'" to display the column names of a table. Dont forget to change the displaymember and valuemember

This article has been dead for over six months. Start a new discussion instead.