Link to home
Start Free TrialLog in
Avatar of dpicco
dpiccoFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Ramkisan Jagtap
Ramkisan Jagtap
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi
You Please replace your code with
Following One

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


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