Combox's value field

I have an Ajax combo box. Call the database and add items to it. Then I look at the source code and the value should have Ids in it but it has text in them...

<option selected="selected" value="Select HCprovider">Select HCprovider</option>
      <option value="Bob1 Jones">Bob1 Jones</option>
      <option value="pp333 ppp">pp333 ppp</option>
      <option value="officer 1 llll">officer 1 llll</option>


So, I bind, then remove duplicates from the combo box. I have the code below. Not sure why it ends up with the text ...instead of the value...
--- in aspx
 <asp:ComboBox ID="cbProvider" DataTextField="HCproviders" DataValueField="hcIds" AutoCompleteMode="SuggestAppend" CaseSensitive="false" DropDownStyle="DropDownList"  runat="server">
             </asp:ComboBox>

--- adding items to the combo box

 var results = pd.LoadOfficeLocationHCprovidersByState(UserSession,int.Parse(ddlState.SelectedValue));

        foreach (var p in results)
        {
            if (p.HCproviders.Contains(","))
            {
                String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                foreach (String i in providerValues)
                {
                    foreach (string j in idValues)
                    {
                       // rdProvider.Items.Add(new RadComboBoxItem(i, j));
                        cbProvider.Items.Add(new ListItem(i, j));
                    }

                }
            }
            else
            {
                 cbProvider.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));
            }
cbProvider = common.RemoveDuplicates(cbProvider); //remove dups
--- removing duplicates

 public ComboBox RemoveDuplicates(ComboBox comboBox)
        {

            //remove duplicate items from list
            List<String> Removed = new List<String>();
            for (int I = 0; I < comboBox.Items.Count; I++)
            {
                if (!Removed.Contains(comboBox.Items[I].ToString()))
                {
                    Removed.Add(comboBox.Items[I].ToString());
                }
            }
            comboBox.Items.Clear();
            foreach (String S in Removed)
            {
                comboBox.Items.Add(S.Trim());
            }

            return comboBox;
        }

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Easwaran ParamasivamCommented:
 Debug your code. Place the breakpoint at the line 20 and 27 in your code and see what the value is getting stored in the combobox. It should be in the format (id,value).

  I suspect that the id value is not getting captured properly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dexterrajeshCommented:
hi,

the ListItem constructor you have used is of format new ListItem(text, value)

in you code below might be j having the text and i having the value

cbProvider.Items.Add(new ListItem(i, j));

if so.. then set them properly...
0
CamilliaAuthor Commented:
I put a debug step (below) ...the values of j and i are correct but when added to the combo, the "value" has text in it!

This is what I see:

in loop i -- Bob1 Jones
in loop j -- 1
text--Bob1 Jones
value--Bob1 Jones

I even added this to the code:
cbProvider.DataTextField = "i";
 cbProvider.DataValueField = "j";
foreach (String i in providerValues)
                {
                    foreach (string j in idValues)
                    {
                       // rdProvider.Items.Add(new RadComboBoxItem(i, j));
                        cbProvider.DataTextField = "i";
                        cbProvider.DataValueField = "j";
                        cbProvider.Items.Add(new ListItem(i, j));
                        
                        Response.Write("in loop i -- " + i + "<br/>");
                        Response.Write("in loop j -- " + j + "<br/>");
                        Response.Write("text--" + cbProvider.Items[0].Text + "<br/>");
                        Response.Write("value--" + cbProvider.Items[0].Value + "<br/>");
                    }

                }

Open in new window

0
CamilliaAuthor Commented:
this is what i did, seems to be working

 ListItem item = new ListItem();
                        item.Value = j;
                        item.Text = i;
                        cbProvider.Items.Add(item);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.