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

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":

Comments

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

1 Comment

Just FYI, you're answering a year-old question, and your answer duplicates the accepted answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.