DataBinding and drop-down lists

S

Sankar

There is no dearth of this subject in this group, but I am not sure if
it answers this question. If this question is stupid, please let me
know it is and excuse my ignorance.

SQL Query: select user_id,first_name,last_name from user

I have a drop-down list that needs to fill in the name of the user in
the format "FirstName LastName" using standard databinding techniques.
This is the code that I have

A dataReader contains the resultset.

cboUser.DataSource = dataReader
cboUser.DataValueField = "user_id"

I do not know how to get the "FirstName LastName" in the dropdown list.
I tried something like

cboUser.DataTextField = String.Concat("first_name"," ","last_name")

and it is of no avail.

An error is thrown on the call to DataBind() and the message says that
"first_name last_name" is not comprehensible.

I am not at liberty to change the sql query to do the concatenation.
Is there a way out?

Thanks,
-Sankar
 
J

Jeppe Dige Jespersen

I am not at liberty to change the sql query to do the concatenation.
Is there a way out?

Well.... if you have the option of getting kinda dirty and use a dataAdapter
and a dataset to fill your dropdownlist, this could work:


ds.tables("Customers").Columns.Add("OurNewCombinedColumn",
GetType(System.String), "[CompanyName] + ', ' + [ContactName]"
DropDownList1.DataSource = ds
DropDownList1.DataTextField = "NewColumn"
DropDownList1.DataBind()

Hope it helps.

Jeppe Jespersen
 
S

Sankar

Hi Jeppe,

Thanks for the information. I could accomplish what I needed with your
idea.

It is hard for me to believe that it is convoluted though :)
Thanks again. I appreciate it.

-Sankar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top