• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

combine 2 datasources for a dropdownlist c#

How can I use 2 sqldatasources to populate a dropdownlist? This is the relevant code I have but it's not working. Thanks.

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                DataTextField="Name" DataValueField="Name">
            </asp:DropDownList>
 <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
                DataTextField="Name" DataValueField="Name" Visible="true">
            </asp:DropDownList>


            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BRConnectionString %>"
                SelectCommand="select Name from Company"></asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BRNewConnectionString %>"
                SelectCommand="select Name from Company"></asp:SqlDataSource>


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            foreach (var item in this.DropDownList2.Items)
            {
                this.DropDownList1.Items.Add(item.ToString());
            }

        }
    }

Open in new window

0
dpicco
Asked:
dpicco
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
add a attribute to dropdownlist
AppendDataBoundItems=true
0
 
AlokJain0412Commented:
Hi
You Please replace your code with
Following One

foreach (Listitem item in this.DropDownList2.Items)
            {
                this.DropDownList1.Items.Add(item);
            }


0
 
jagssiduralaCommented:
Try the above items appending in Page_loadcomplete event. It will works....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now