1

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'."

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
user3147424
  • 3,022
  • 5
  • 19
  • 22

3 Answers3

2

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

Gerf
  • 311
  • 1
  • 4
1

Try:

customerselect.DataTextField = string.Format("{0}+ | +{1}", Name, Number);
MTAdmin
  • 1,023
  • 3
  • 17
  • 36
0

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 . . ." 
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43