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#?

Recommended Answers

All 2 Replies

    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);
                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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.