开发者

Error in DropDownList using ASP.NET

开发者 https://www.devze.com 2023-01-01 10:19 出处:网络
I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消