Solved

combine 2 datasources for a dropdownlist c#

Posted on 2011-09-30
3
223 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
Comment Utility
add a attribute to dropdownlist
AppendDataBoundItems=true
0
 
LVL 5

Expert Comment

by:AlokJain0412
Comment Utility
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
Comment Utility
Try the above items appending in Page_loadcomplete event. It will works....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

17 Experts available now in Live!

Get 1:1 Help Now