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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:22:03+00:00 2026-05-13T13:22:03+00:00

I have this code in a business class. internal ListItemCollection GetAllAgents() { DataTable table

  • 0

I have this code in a business class.

    internal ListItemCollection GetAllAgents()
    {
        DataTable table = dao.GetAllAgents();
        ListItemCollection list = new ListItemCollection();

        foreach (DataRow row in table.Rows)
        {
            list.Add(new ListItem(row["agent_name"].ToString(), row["id"].ToString()));
        }
        return list;
    }

I get the table back from the dao without issue. I watch the text and values properties populate properly(+1 for some awesome illiteration?) and returned to the presentation and I bind like this

 Helper helper = new Helper();
 ListItemCollection agentList = helper.GetAllAgents();
 agentList.Insert(0,"");
 this.ddlAgent.DataSource = agentList;
 this.ddlAgent.DataBind();

when I make get the selected value

this.ddlAgent.SelectedValue

I would expect to see the agent id, but what I get is the text (agent name), so I tried this

this.ddlAgent.SelectedItem.Value

but I got the same results. I then took a look at the html source being generated and it looks like this

<select name="ctl00$ContentPlaceHolder1$ddlAgent" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$ddlAgent\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_ddlAgent">
        <option selected="selected" value=""></option>
        <option value="agent1_name">agent1_name</option>
        <option value="agent2_name">agent2_name</option>

this pattern continues for all the agents. I’m hoping I’m just doing something bone headed and you can all snicker as you solve my problem 🙂

Thanks guys.

EDIT: if I do it like this

ListItemCollection agentList = helper.GetAllAgents();
agentList.Insert(0,"");
foreach (ListItem agent in agentList)
{
    this.ddlAgent.Items.Add(agent);
}

it works fine.

  • 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-13T13:22:04+00:00Added an answer on May 13, 2026 at 1:22 pm

    Try doing:

    this.ddlAgent.DataTextField = "Text";
    this.ddlAgent.DataValueField = "Value";
    this.ddlAgent.DataSource = agentList;
    this.ddlAgent.DataBind();
    

    Should also work, and it’s probably better than looping through the list for no reason.

    Update Found another (shorter) way of doing this:

    this.ddlAgent.Items.AddRange(agentList.ToArray());
    this.ddlAgent.DataBind();
    

    By using Items.AddRange() instead of setting the source with DataSource, ASP is able to figure out that it should use the Text and Value properties.

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

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You do this by implementing View#onSaveInstanceState and View#onRestoreInstanceState and extending… May 16, 2026 at 12:21 pm
  • Editorial Team
    Editorial Team added an answer As Martin stated your error seems to be centered around… May 16, 2026 at 12:21 pm
  • Editorial Team
    Editorial Team added an answer What I ended up doing was setting the chart type… May 16, 2026 at 12:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have this code: using DC = MV6DataContext; using MV6; // Business Logic Layer
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
I have this code: if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Orange) == KeyState.Lock) PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Orange, 0, 0); if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Blue) ==
I have this code in my controller for Index view.. public ActionResult Index(int? id)
I am creating a series of UserControls that all have some similar business logic.
We have encountered a very strange class not found problem in our web app
In brief: Is it ever excusable to have assembly names and class names stored
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Recently I've been thinking about securing some of my code. I'm curious how one
I'm trying to build a proof of concept Csla 3.7/Silverlight 3 app and have

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.