tags:

views:

20

answers:

2

Suppose in .aspx page their is dropdown in which i want to bind two coloumn in that dropdown.for example :-

in database their two column of First_name ,Last_name .I want these two columns value come in a single dropdown using C#.

How to do that? Please tell me.

+2  A: 

If you are using the sql query , try this:

select First_name  + ' ' + Last_name   from table

Then you need to bind it in the Dropdown as normally.

dropdown1.DataSource = datatable;
dropdown1.DataBind();
anishmarokey
SIR it gives error that datatable does not exit in current context.
Shalni
@Shalni - that may be because u just copied anishmarokey's code as is in your application. Unless your own objects were named exactly the same, it would give you the error that you are seeing. anishmarokey's suggestion is absolutely spot on but you need to map the object names "table" "datatable" "dropdown1" to whatever you are calling them in your code or else post your actual code atleast
InSane
A: 

An alternative to doing it in code would be to drop a SQL data source on the page and configure in the following way.

<asp:sqldatasource id="SqlDataSource1" runat="server" 
connectionstring="<%$ ConnectionStrings:MyDatabase %>" 
selectcommand="SELECT [ID], [First_name] + ' ' + [Last_name] AS [FullName] FROM [tPerson]"></asp:sqldatasource>

And then your dropdown list control:

<asp:dropdownlist id="ddlPeople" runat="server" datasourceid="SqlDataSource1"
    datatextfield="FullName" datavaluefield="ID" />

This would all go in the asp.net page.

Naeem Sarfraz