Search code examples
asp.netdatareadersystem.data

Error in DropDownList using ASP.NET


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

Solution

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