Q:

Get database table Columns?

Hi i have a table in the data base with some columns i want to bind only the column headers to the dropdownlist........
i had tried in way which is working in way which is hard code
this is code which is excueted
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Data;
      using System.Web.UI;
      using System.Web.UI.WebControls;
       using System.Data.OleDb;

      namespace WebApplication1
     {
    public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Excelpath = Server.MapPath("~/ExcelFiles/TaskSheet.xlsx");
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excelpath + ";Extended Properties='Excel 12.0;HDR=YES;'";
        OleDbDataAdapter DB = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", new OleDbConnection(connString));

        System.Data.DataSet DS = new System.Data.DataSet();
        DB.Fill(DS);
        Table t = new Table();

        foreach (DataTable table in DS.Tables) { 
            foreach (DataColumn column in table.Columns)
            {
                DropDownList list = new DropDownList(); list.Items.Add("Name"); list.Items.Add("Date"); list.Items.Add("Task");
                Label l = new Label(); l.Text = column.ColumnName;
                TableRow r = new TableRow();
                TableCell c = new TableCell();
                c.Controls.Add(l);

                TableCell c1 = new TableCell();
                c1.Controls.Add(list);

                r.Cells.Add(c);
                r.Cells.Add(c1);
                t.Rows.Add(r);

            }
        }
        Page.Form.Controls.Add(t);
    }
}
}

instead of binding each and single column i want to loop that
i tried this
      public partial class WebForm1 : System.Web.UI.Page
          {
       protected void Page_Load(object sender, EventArgs e)
         {
          string Excelpath = Server.MapPath("~/ExcelFiles/TaskSheet.xlsx");
           string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +     Excelpath + ";Extended Properties='Excel 12.0;HDR=YES;'";
        OleDbDataAdapter DB = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", new OleDbConnection(connString));

        System.Data.DataSet DS = new System.Data.DataSet();
        DB.Fill(DS);
        Table t = new Table();

        foreach (DataTable table in DS.Tables) 
        { 
            foreach (DataColumn column in table.Columns)
            {
                string insertstring = @"select * from CUSTOMER_DETAILS1";
                SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS");
                conn.Open();
                SqlCommand cmd = new SqlCommand(insertstring, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet data = new DataSet();
                adapter.Fill(data);
                conn.Close();
                DropDownList list = new DropDownList();
                list.DataSource = data.Tables[0];
                list.Items.Add("data");
                list.DataBind();

                //ddlFrom.DataSource = data.Tables[0];
                //ddlFrom.DataValueField = "FromId";
                //ddlFrom.DataTextField = "From";
                //ddlFrom.DataBind();
               // DropDownList list = new DropDownList(); list.Items.Add("Name"); list.Items.Add("Date"); list.Items.Add("Task");
                Label l = new Label(); l.Text = column.ColumnName;
                TableRow r = new TableRow();
                TableCell c = new TableCell();
                c.Controls.Add(l);

                TableCell c1 = new TableCell();
                c1.Controls.Add(list);

                r.Cells.Add(c);
                r.Cells.Add(c1);
                t.Rows.Add(r);

            }
        }
        Page.Form.Controls.Add(t);
    }

}
}

i dnt have any error in this but i cant see the cloumns in the dropdownlist...can any one help me

A:

Assuming you are using SQL Server (big assumption)
This SQL will get you a list of column headers for a table that you can bind to your drop down list
select COLUMN_NAME from INFORMATION_SCHEMA.Columns
where Table_Name = 'MyTable'

Will get you the drop down with a list of headers - not sure if that moves you forward so much
EDIT:
Oops - you're using access aren't you. This is your nearest to INFORMATION_SCHEMA to get database metadata
OleDbConnection.GetOleDbSchemaTable()

with this code to get a list of columns
cn.Open()

'Retrieve schema information about columns.
'Restrict to just the Employees TABLE.
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, _
              New Object() {Nothing, Nothing, "Your table", Nothing})

'List the column name from each row in the schema table.
For i = 0 To schemaTable.Rows.Count - 1
    Console.WriteLine(schemaTable.Rows(i)!COLUMN_NAME.ToString)
Next i

'Explicitly close - don't wait on garbage collection.
cn.Close()

That I found here. Sorry it's in VB.Net - easy enough job to translate into C# as required.

