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 9177779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:24:14+00:00 2026-06-17T17:24:14+00:00

I am trying to populate a ddl (which works) and when I select the

  • 0

I am trying to populate a ddl (which works) and when I select the ‘FirstName’ the textboxes are populated for editing.
When i step through the program the (drp_Customer.SelectedItem.Value) is always ’23’ which is the last ‘Id’ in my DB.
The fileds get populated but with 23’s details..

I am thinking I need to make sure the ddl’s details match up.

Here is my code.
It is only rough as i am trying to show somebody how it might work.

            using (CustomerDataContext obj = new CustomerDataContext())
        {
            // Get fields for drp_Customer
            // ===========================
            var allCustomers = from c in obj.Customers
                               orderby c.FirstName
                               select new
                                   {
                                       c.FirstName,
                                       c.CustomerId
                                   };

            foreach (var item in allCustomers)
            {
                drp_Customer.Items.Add(item.FirstName);
                drp_Customer.SelectedItem.Value = item.CustomerId.ToString();
            }

            drp_Customer.DataBind();

            if (drp_Customer.SelectedItem.Text == " -- Select Customer -- ")
            {
                lbl_message.Text = "Please select a customer to update";
            }
            else if (drp_Customer.SelectedItem.Text != " -- Select Customer -- ")
            {

       Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId ==  Convert.ToInt32       
            (drp_Customer.SelectedItem.Value));

                if (myCust != null)
                {
                    txt_FirstName.Text = myCust.FirstName;
                    txt_Surname.Text = myCust.Surname;
                    txt_HouseNumber.Text = myCust.HouseNumberName;
                    txt_Address.Text = myCust.Address;
                    txt_Town.Text = myCust.Town;
                    txt_Telephone.Text = myCust.Telephone;
                    txt_Postcode.Text = myCust.Postcode;
                }

            }
  • 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-06-17T17:24:15+00:00Added an answer on June 17, 2026 at 5:24 pm

    The reason why always the last item is selected is this loop:

    foreach (var item in allCustomers)
    {
        drp_Customer.Items.Add(item.FirstName);
        drp_Customer.SelectedItem.Value = item.CustomerId.ToString();
    }
    

    The last item will be the selected item since you’re always changing it. You need to assign it outside of the loop. But you haven’t shown which customer is the correct selected customer currently.

    Note that you also can use DataSource + DataBind:

    drp_Customer.DataTextField = "FirstName";
    drp_Customer.DataValueField = "CustomerId";
    drp_Customer.DataSource = allCustomers;
    drp_Customer.DataBind();
    drp_Customer.SelectedValue = customerIDofSelectedCustomer;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate $('div').data() through JSON. It works fine with JQuery.parseJSON but
I am trying to populate a combobox, which is part of an itemscontrol, with
I am trying to populate dictionary object at client side which i can pass
I'm trying to populate a second drop down list (which contains counties) depending on
What I am trying to do is populate data in a select element. I'm
I am trying to populate an HTML select list using an array of customer
Trying to populate select with sql results using WebMethods. I can get and pass
I am trying to populate a select list ( Zend_Form_Element_Select ) and i have
I am trying populate H3 with the sum of column E for rows that
I trying to populate the dropdown list when page was loaded.But it is not

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.