Populate DataTextField with "Example Name | Example Number"

121 views Asked by At

I have a SQL select statement and using the results I bind it to an asp dropdown.

String strQuery ="SELECT RTRIM(NAME), NUMBER . . ." 

customerselect.DataTextField = "NAME+ ' | ' +NUMBER";

I need to display in the DataTextField:

Example Name | 123456

I'm getting an error:

" does not contain a property with the name 'NAME+ ' | ' +NUMBER'."

3

There are 3 answers

0
MTAdmin On

Try:

customerselect.DataTextField = string.Format("{0}+ | +{1}", Name, Number);
0
Dan Bracuk On

Give your function result an alias. In other words, change this:

String strQuery ="SELECT RTRIM(NAME), NUMBER . . ." 

to this:

String strQuery ="SELECT RTRIM(NAME) as name, NUMBER . . ." 
0
Gerf On

The DataTextField property specifies which field from your data source to use as the text label for the drop-down, not the actual text string itself. So, if you wanted the drop-down to have that particular format, you might try something like the following:

String strQuery = "SELECT RTRIM(NAME) + ' | ' +
    CAST(NUMBER AS VARCHAR(32)) AS Label, NUMBER AS Value . . .";

// Run the query and do the appropriate data binding here

customerselect.DataTextField = "Label";

Additional info on the DataTextField property can be found here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datatextfield%28v=vs.110%29.aspx