Solved

Bind string array to a drop down list

Posted on 2006-06-13
12
770 Views
Last Modified: 2012-05-05
What is the proper syntax for what I'm trying to do?  LinkUser returns a string array which I'm trying to bind to a dropdown list..

private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      ddlAgencyCode.DataValueField      = me.agencyCodes[0];
      ddlAgencyCode.DataTextField      = me.agencyName[0].ToString().Trim();
      
      ddlAgencyCode.DataSource = me;
      ddlAgencyCode.DataBind();
}
0
Comment
Question by:russomr
[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
  • 4
  • 4
  • 4
12 Comments
 
LVL 4

Expert Comment

by:svy
ID: 16896631
private void PopulateAgencies(){
     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();
}
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 250 total points
ID: 16896655
I would do it like this

private void PopulateAgencies(){
for(int i = 0; i< agencyCodes.length;i++)
{
ListItem MyItem = new ListItem();
MyItem.Text = agencyCodes[i].ToString();
MyItem.Value = agencyCodes[i];
ddlAgencyCode.Items.Add(MyItem);
}

}
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896662
svy, how would you differentiate between a string value and an int value for the text and value properties? i.e. russomr seems to want an int for the value and a string for the text.

:-)

Andrew
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896668
My method enables you to apply each format
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896673
sorry I mean each cast not format :-(
0
 
LVL 4

Expert Comment

by:svy
ID: 16897201
REA_ANDREW, ListItem.Value and ListItem.Text are both string type properties. I'm not sure what you're tryint to do with your code there...?

There's no need to specify DataTextField and DataValueField in this case.
0
 

Author Comment

by:russomr
ID: 16897220
svy,

the code

     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();

seems to work, accept how do I specify it so that the ddl value is the agencyCodes and what is displayed in the ddl is the agencyName?  Right now it just displays the agencyCodes.

Thanks.

0
 
LVL 4

Expert Comment

by:svy
ID: 16897675
private IEnumerable GetPairs(agencyCodes, agencyNames)
{
for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
yield return new Pair(agencyCodes[i], agencyNames[i]);
}
private void PopulateAgencies(){
     string [] agencyCodes = new LinkUser().agencyCodes;
     string [] agencyNames = new LinkUser().agencyNames;
     ddlAgencyCode.DataTextField = "Second";
     ddlAgencyCode.DataValueField = "First";
     ddlAgencyCode.DataSource = GetPairs(agencyCodes, agencyNames);
     ddlAgencyCode.DataBind();
}
0
 

Author Comment

by:russomr
ID: 16904376
I get this error message:

Compiler Error Message: CS1001: Identifier expected

(line 31 is the bolded line)

Source Error:

Line 29: }
Line 30:
Line 31: private IEnumerable GetPairs(agencyCodes, agencyNames){
Line 32:       for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
Line 33:       yield return new Pair(agencyCodes[i], agencyNames[i]);
 
I'm doing in-line coding.  Not sure if that makes a difference.
0
 

Author Comment

by:russomr
ID: 16904482
REA_ANDDREW,

I have used your code and applied it like so:

*********************************************
private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      for(int i = 0; i< me.agencyCodes.length;i++){
            ListItem MyItem = new ListItem();
            MyItem.Text = me.agencyNames[i].ToString();
            MyItem.Value = me.agencyCodes[i].ToString();
            ddlAgencyCode.Items.Add(MyItem);
      }

}
*********************************************

Unfortunately, I get the following error:

*********************************************
Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'length'

Source Error:

Line 48:       LinkUser me = new LinkUser();
Line 49:       
Line 50:       for(int i = 0; i< me.agencyCodes.length;i++){
Line 51:             ListItem MyItem = new ListItem();
Line 52:             MyItem.Text = me.agencyNames[i].ToString();
*********************************************
 
Any thoughts?
0
 

Author Comment

by:russomr
ID: 16904524
Aaaah.  Capital L would help.  I think I got it going.  Thanks for all your help.
0
 
LVL 4

Expert Comment

by:svy
ID: 16905547
private IEnumerable GetItems(string[] agencyCodes, string[] agencyNames)
      {
            for (int i = 0; i < agencyCodes.Length && i < agencyNames.Length; i++)
                  yield return new ListItem(agencyNames[i], agencyCodes[i]);
      }
      private void PopulateAgencies()
      {
            string [] agencyCodes = new LinkUser().agencyCodes;
            string [] agencyNames = new LinkUser().agencyNames;

            ddlAgencyCode.DataTextField = "Text";
            ddlAgencyCode.DataValueField = "Value";
            ddlAgencyCode.DataSource = GetItems(agencyCodes, agencyNames);
            ddlAgencyCode.DataBind();
      }
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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