1

I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.

So I did this :

DDL.DataSource = myDataReader

DDL.DataBind()

But it's giving me (5 records) "the number of records of the table" but like this :

System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

3 Answers3

4

You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:

DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
tpeczek
  • 23,867
  • 3
  • 74
  • 77
0

you have to set the text and the key fields of the ddl before you databind

DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
derek
  • 4,826
  • 1
  • 23
  • 26
0

The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.
Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.
So you will write: ddl.textfield="column name which you want to show"; and ddl.valuefield="column name which you want as a reference to pass to database";

Echilon
  • 10,064
  • 33
  • 131
  • 217
  • Just FYI, you're answering a year-old question, and your answer duplicates the accepted answer. :) – Andomar Mar 03 '11 at 06:37