Click here to Skip to main content
15,895,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to fill dropdown from excel column.
Every thing was fine ,but it's displaying system.data.datarow instead of Values in dropdown list.

wat's the mistake in the following code.

C#
           DataTable dt = new DataTable();
          string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\OnCall Tracker_2011.xls;Extended Properties='Excel 8.0;HDR=NO'"; 
            using (OleDbConnection conn = new OleDbConnection(connString))
            {   
                conn.Open();    
                //Where [F1] = column one, [F2] = column two etc, etc.              
                //OleDbCommand objCmdSelect = new OleDbCommand(sqlQuery, conn);                   
                //objCmdSelect.ExecuteNonQuery();
                string sqlQuery = "select distinct [F1] AS [id] from [July -11$]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, conn);
                adapter.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
}


Thanks In Advance
Subhash G
Posted
Updated 25-Aug-11 0:29am
v6

Try this
C#
DropDownList1.DataTextField = "id";
DropDownList1.DataValueField = "id";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
 
Share this answer
 
v2
Comments
walterhevedeich 25-Aug-11 3:26am    
My 5.
subhash04573 25-Aug-11 6:13am    
Thanks.
And small suggestion on this,
How can i eliminate duplicates values in the column.
subhash04573 25-Aug-11 6:18am    
ya i got it.
Any way thanks .
Prerak Patel 25-Aug-11 6:29am    
Nice, you are welcome. Mark the answer if it is solved.
Try adding the following code below DropDownList1.DataSource = dt;
C#
DropDownList1.DataTextField= dt.Columns[0].ToString();
DropDownList1.DataValueField = dt.Columns[0].ToString();
 
Share this answer
 
Comments
Prerak Patel 25-Aug-11 4:49am    
My 5. I was under impression on ComboBox and suggested DisplayMember and ValueMember.
subhash04573 25-Aug-11 6:13am    
Thanks,
And small suggestion on this, How can i eliminate duplicates values in the column.
subhash04573 25-Aug-11 6:18am    
ya i got it.
Any way thanks .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900