Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format DropDownList.TextValue

My stored procedure is like this

SELECT Id, StudentName
FROM xyz

I have a drop down list in asp.net, which I am loading as :

ddlA.DataSource = // Some source
ddlA.DataTextField = "Id" + " -" + "StudentName";
ddlA.DataValueField = "Id";
ddlA.DataBind();
ddlA.Items.Insert(0, new ListItem(" Select one", "0"));

But at the Databind() statement, I am getting this error:

System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Id-StudentName'.

In text part of the dropdown list, I want to display the concatenated value of Id - StudentName.

How can I do it?

like image 1000
Zerotoinfinity Avatar asked Jan 25 '11 14:01

Zerotoinfinity


Video Answer


2 Answers

If the DataSource is a DataTable, you can add an "expression" (calculated column) to the table. The expression for the new column would look like this:

newcolumn.Expression = "Id + ' - ' + StudentName";
like image 149
Captain Sensible Avatar answered Oct 19 '22 18:10

Captain Sensible


You can change your stored procedure to:

SELECT Id, Id + " - " StudentName as Text FROM xyz

And change the binding to:

ddlA.DataSource = // Some source
ddlA.DataTextField = "Text"
ddlA.DataValueField = "Id";     
ddlA.DataBind();     
ddlA.Items.Insert(0, new ListItem(" Select one", "0")); 
like image 23
Pabuc Avatar answered Oct 19 '22 17:10

Pabuc