Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7176681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:33:32+00:00 2026-05-28T16:33:32+00:00

I load cities from the data context into two different lists and bind them

  • 0

I load cities from the data context into two different lists and bind them to their respective DropDownList controls.

Although the code is the same for both, and only the data and obviously the name of the controls are different, the data binding seems to not work properly for only one of them. Instead of displaying the city name, it displays its Id, that is, for the first option only!

We have two DropDownList controls:

  1. DestinationDropDownList;
  2. OriginDropDownList.

Then, I populate them.

public partial class MyControl : UserControl {
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            var instruction = new City() {
                CityId = Guid.Empty,
                CityName = "- Select a city -"
            };

            var destinations = context.Cities.ToList();
            destinations = destinations.OrderBy(c => c.CityName).ToList();
            destinations.Insert(0, instruction);
            DestinationDropDownList.DataSource = destinations;
            DestinationDropDownList.DataTextField = "CityName";
            DestinationDropDownList.DataValueField = "CityId";
            DestinationDropDownList.DataBind();

            var origins = context.Cities.ToList();
            origins = origins.OrderBy(c => c.CityName).ToList();
            origins.Insert(0, instruction);
            OriginDropDownList.DataSource = origins;
            OriginDropDownList.DataTextField = "CityName";
            OriginDropDownList.DataValueField = "CityId";
            OriginDropDownList.DataBind();
        }
    }
    private static readonly MyDataContext context = new MyDataContext();
}

HTML:

<asp:DropDownList ID="DestinationDropDownList" runat="server" />
<asp:DropDownList ID="OriginDropDownList" runat="server" />

Data displayed:

*DestinationDropwDownList*
    - 0000-0000-0000-0000-0000 (empty Guid)
    - CityName 01
    - CityName 02
    - ...

*OriginDropDownList*
    - - Select a city -
    - CityName 01
    - CityName 02
    - ...

The correct display is the one rendered by OriginDropDownList control. Why is DestinationDropDownList not displaying the data correctly, that is, for the first and only the first item in its list?

  1. I tried removing the first item before inserting the instruction;
  2. I tried to insert the instruction twice, then remove the first one;
  3. I tried to go along with the DropDownList.Items.Add(string) method, and this was displaying nothing as for the first row, instead the the empty Guid.

How can I correct this behaviour!?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:33:33+00:00Added an answer on May 28, 2026 at 4:33 pm

    I haven’t encountered this issue before, but I will try to answer your second question. I see little value in adding the first ‘instructional’ item through code (correct me if I’m wrong). I’d rather add it directly to the mark-up:

    <asp:DropDownList ID="DestinationDropDownList" runat="server"
      AppendDataBoundItems="true"
      DataTextField="CityName"
      DataTextField="CityId">
      <asp:ListItem Text="- Select a city -" />
    </asp:DropDownList>
    

    The first item will survive the data binding due to the use of the AppendDataBoundItems attribute. I also added the DataTextField and DataValueField attributes, you can remove those from the code as well.

    Another thing you can do before data binding, is projecting the entities to ListItem instances:

    DestinationDropDownList.DataSource = 
      from d in destinations
      select new ListItem() {
        Text = d.CityName,
        Value = d.CityId.ToString()
      };
    DestinationDropDownList.DataBind();
    

    This way, you don’t have to set the DataTextField or DataValueField properties on the control as you are already creating the list’s items yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I load data into my grid using a WCF service. When a user clicks
ViewA load some data from Coredata, then push to the next viewB . In
I'm trying to load data from database and display it in table like here:
Load multile SWF files from XML data and upon NEXT and BACK button it
load different list of values(from a database) to listbox when changing another listbox value
This is really strange. I want to select a State and load Cities from
I am trying to load a series of cities/countries in a textbox to display
I load a UIWebView from a string in a navigation based app. I'm using
I use this code to load an Excel file into a datatable: public static
I have a problem with saving data from Ext.data.DirectStore to server Here is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.