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?
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";
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With