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

Dani AI

Generated

Good start: demonstrates the WinForms binding flow and points toward the information-schema approach. For SQL Server the reliable way to get column names is to read the metadata view (not a column called name) — e.g. query the INFORMATION_SCHEMA.COLUMNS (or sys.columns) for the target TABLE_SCHEMA/TABLE_NAME and order by ORDINAL_POSITION so the columns come back in table order. See an official example for getting column names. (learn.microsoft.com)
INFORMATION_SCHEMA.COLUMNS (example and alternatives)

A simple, safe C# pattern: run a parameterized metadata query, read the COLUMN_NAME values and add them to the ComboBox (or bind a list). Use using so connections/commands/readers are disposed, and pass table/schema as parameters (never concatenate user input into SQL). Example pattern (adjust names and schema as needed):

using (var conn = new SqlConnection(connString))
using (var cmd  = conn.CreateCommand())
{
  cmd.CommandText = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=@schema AND TABLE_NAME=@table ORDER BY ORDINAL_POSITION";
  cmd.Parameters.AddWithValue("@schema", "dbo");
  cmd.Parameters.AddWithValue("@table", "YourTable");
  conn.Open();
  using (var rdr = cmd.ExecuteReader())
    while (rdr.Read())
      comboBox1.Items.Add(rdr.GetString(0));
}

Use parameterized commands to avoid SQL injection and to get proper plan reuse. (learn.microsoft.com)
ADO.NET parameter guidance
C# using statement / disposal

Troubleshooting tips: DisplayMember and ValueMember must match the column name exactly (case matters — a mismatch often yields "System.Data.DataRowView" entries). If you bind a DataTable, set DisplayMember/ValueMember carefully; many developers set those before assigning DataSource to avoid rebinding/event surprises. Check that the connection context is the correct database (INFORMATION_SCHEMA is database-scoped). (fmsinc.com)
ComboBox binding notes and case-sensitivity
ComboBox DisplayMember/ValueMember ordering discussion

Extra: for heavy UIs cache the column names (they rarely change), limit to the schema(s) users need, or use sys.columns / sp_columns if you need extra metadata (types, nullability). (learn.microsoft.com)

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

Be a part of the DaniWeb community

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