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 givi开发者_StackOverflowng 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
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();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
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";
精彩评论