Solved

combine 2 datasources for a dropdownlist c#

Posted on 2011-09-30
3
224 Views
Last Modified: 2012-05-12
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
Comment
Question by:dpicco
3 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 36895249
add a attribute to dropdownlist
AppendDataBoundItems=true
0
 
LVL 5

Expert Comment

by:AlokJain0412
ID: 36896039
Hi
You Please replace your code with
Following One

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


0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36897391
Try the above items appending in Page_loadcomplete event. It will works....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now