Solved

combine 2 datasources for a dropdownlist c#

Posted on 2011-09-30
3
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

689 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