locked
Dropdownlist gives 'SelectedValue which is invalid because it does not exist in the list of items' RRS feed

  • Question

  • User-1513921738 posted

    Hi,

    I have a dropdownlist which is populated by a SQL Server table.  It literally has the strings "1", "2", "3", "4" & "5" for categories.  I click a Gridview and get a value of 1 through 5, and I can't figure out how to force the dropdownlist to go to number "3" for example  The Text and values in the list are the same.  The list.count = 5, so I know the stuff should be there.  I've tried this:

    DropDownListCat.SelectedValue = "3";

    But I get the error that 'SelectedValue which is invalid because it does not exist in the list of items'.

    It is a VS10 Web project using c# and SQL Server ...

    Thank you ...

    Wednesday, May 2, 2012 7:55 PM

Answers

  • User-1513921738 posted

    I found the problem through debugging.  I am going to detail it in case someone else may see this.  I have the number 1 though 5 stored in char(4) record ina SQL server table.  When the list is populated from the datasource, the trailing spaces exist.  I'm sure there is a better way in one line of code to set the selection, but I used a while loop to trim the data.  After spending two hours on this, I have to move on ...

     

    string sCat = reader[7].ToString().Trim();
    int iNdex = 0;
    bool bLoop = true
    string sListVal;

    // Find the cat
    while (bLoop)
    {
         DropDownListCategory.SelectedIndex = iNdex;
         sListVal = DropDownListCategory.SelectedValue.ToString().Trim();

         if(sCat == sListVal)
              bLoop = false;

         iNdex += 1;
    }

    DropDownListCategory.SelectedIndex = iNdex - 1;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 3, 2012 12:37 PM

All replies

  • User-232462845 posted

    Will you please show how and in which event  do you populate the dropdownlist.

    Most probably at the time you set the selectedvalue the DDL may not be  got populated.

    Wednesday, May 2, 2012 9:10 PM
  • User-1513921738 posted

    I found the problem through debugging.  I am going to detail it in case someone else may see this.  I have the number 1 though 5 stored in char(4) record ina SQL server table.  When the list is populated from the datasource, the trailing spaces exist.  I'm sure there is a better way in one line of code to set the selection, but I used a while loop to trim the data.  After spending two hours on this, I have to move on ...

     

    string sCat = reader[7].ToString().Trim();
    int iNdex = 0;
    bool bLoop = true
    string sListVal;

    // Find the cat
    while (bLoop)
    {
         DropDownListCategory.SelectedIndex = iNdex;
         sListVal = DropDownListCategory.SelectedValue.ToString().Trim();

         if(sCat == sListVal)
              bLoop = false;

         iNdex += 1;
    }

    DropDownListCategory.SelectedIndex = iNdex - 1;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 3, 2012 12:37 PM