yrazlik
yrazlik

Reputation: 10777

Connecting a data source to dropdown list in asp.net

I am trying to connect a data source to a dropdown list. I have people called "instructors" in my database and i want their names and surnames in my dropdown list. Here is the related part of code:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [Name], [Surname] FROM [InstructorTable] ORDER BY [Name]">
    </asp:SqlDataSource>

The problem is, i only see their names, not surnames in the dropdown list. What can be the problem here? Can anyone help?

Thanks

Upvotes: 0

Views: 1037

Answers (2)

ebram khalil
ebram khalil

Reputation: 8321

what you are looking for is DropDownList composite datatextfield. This Can be done by Using sql statements.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT [Name] + '' + [Surname] as FullName FROM [InstructorTable] ORDER BY [Name]">
</asp:SqlDataSource>

then in your code DataTextField="FullName"

check binding-multiple-fields-to-listbox-in-asp-net

Upvotes: 1

Pleun
Pleun

Reputation: 8920

Because the dropdown takes only only one field.

You need to concat Name & Surname in the select query and you will be fine.

Update

 SelectCommand="SELECT Concat([Name], [Surname]) as CombinedName FROM [InstructorTable

Upvotes: 2

Related Questions