?
Solved

Bind string array to a drop down list

Posted on 2006-06-13
12
Medium Priority
?
775 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
  • 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

829 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