pre-populate a dropdown list with ID values (int) from a datareader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boriscb
    New Member
    • Dec 2011
    • 5

    pre-populate a dropdown list with ID values (int) from a datareader

    Hi,

    I am having difficulty with trying to pre-populate a dropdown list from a datareader. The list should bind with the ID from a table to display its text value. For example to display the region "Oregon" the ID=5, "Florida" the ID=12 etc...

    this is the code I am using that fails:

    ddlRegion.DataV alueField = rdr.GetOrdinal( "regionID").ToS tring();
    ddlRegion.DataB ind();


    DataBinding: 'System.Data.Da taRowView' does not contain a property with the name '1'

    Your feedback will be greatly appreciated !...
    Thank you.
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    A drop down list, do you mean ComboBox?

    If yes, you can bind it into DataTable.

    Code:
    DataTable dt = new DataTable();
    dt.Columns.Add("Display", typeof(string));
    dt.Columns.Add("Value", typeof(int));
    dt.Rows.Add("Oregon", 5);
    dt.Rows.Add("Florida", 12);
    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = dt.Columns["Display"] + "";
    comboBox1.ValueMember = dt.Columns["Value"] + "";
    
    // To select
    comboBox1.SelectedValue = 5;
    MessageBox.Show(comboBox1.Text);
    
    comboBox1.SelectedIndex = 1;
    MessageBox.Show(comboBox1.Text);
    
    // Getting the selected value
    MessageBox.Show(comboBox1.SelectedValue + "");
    Personally, I prefer to use a dictionary than binding it into a DataTable.

    Comment

    • boriscb
      New Member
      • Dec 2011
      • 5

      #3
      Hi there,

      Thank you for your time and reply. I apologise as I may not have explained my problem correctly so I'll describe the purpose of the code.

      I have an ASP.NET [New Member] form, where I capture club members and on that form there are ASP.NET dropdown lists for Gender and Region.
      The table that fills the [Region] has 2 columns : ID, region
      The member record saves the regionID as an Integer.

      Another page lists in a gridview the individuals [memberID],[names], [Gender], [Region] (which is bibding to the region table by ID) ... The gridview has links on the side to [Edit Member] or [Delete Member]...

      When the [Edit Member] link is clicked, I get the [memberID] from the GridView row and I need to retrieve all its data from the [Member] table to pre-populate the form for editing.

      As long as the data from the Datareader is (string) directly from the [member] table it is working fine, but I need to get the [regionID] to then retrieve the "text" value of the region to display in the [Region] dropdown list as the selected value originally saved.

      That's where my problem lies....

      Thank you for your help and advice with this, and best wishes to you and your loved one over the festive season.

      Boris.

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        I see. You may consider to post this question at the ASP section:

        Comment

        • boriscb
          New Member
          • Dec 2011
          • 5

          #5
          Thank you for your feedback. I will definitely re-post this into the ASP.NET forum as suggested. Once again thank you for your time and take care.

          Boris.

          Comment

          Working...