Question : SelectedValue which is invalid because it does not exist in the list of items

There's an oracle forms application that was built in the 90's and since then a lot of the look up tables have changed (data that existed before does not any more).

I am converting this oracle form to .net and have come across a problem.  One of the look up tables that had 10 records before now has two.  In my asp.net page, I bind a drop down to this table.

The problem:
When I try to populate the drop down with data prvious to the look up table change, I get this error:  
"'ed_caseType' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "

I know exactly what it means.  The data does not exist in the 2 options in the look up table.  I just have not idea of how to fix it.

Any ideas?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
public void dropDownCategory(string catCode)
    {
        String strCategoryCode = String.Empty;
        OracleConnection oracleConn = new OracleConnection();
        oracleConn.ConnectionString = ConfigurationManager.ConnectionStrings["oracleConnectionString"].ConnectionString;
        OracleDataReader ordr;
        string mySql = "SELECT CATEGORY_CD,CATEGORY_DESC,CATEGORY_ID FROM OGC_CATEGORY_LU WHERE CATEGORY_CD = :catCode";
        oracleConn.Open();

        using (OracleCommand cmd = new OracleCommand(mySql, oracleConn))
        {
            cmd.Parameters.AddWithValue(":catCode", catCode.ToString());
            ordr = cmd.ExecuteReader();

            ordr.Read();

            if (strCategoryCode.ToString() == string.Empty)
            {
                strCategoryCode = ordr["CATEGORY_CD"].ToString();
            }
            else
            {
                ed_catCode.Text = ordr["CATEGORY_ID"].ToString();
                ed_catDesc.Text = ordr["CATEGORY_DESC"].ToString();
            }

            ordr.Close();
            oracleConn.Close();
        }
        

        try
        {
            oracleConn.Open();
            string sql = "SELECT CATEGORY_CD,CATEGORY_DESC,CATEGORY_ID FROM OGC_CATEGORY_LU";
            OracleCommand cmdCD = new OracleCommand(sql, oracleConn);
            ed_catCode.DataSource = cmdCD.ExecuteReader();
            ed_catCode.DataTextField = "CATEGORY_CD";
            ed_catCode.DataValueField = "CATEGORY_CD";
            ed_catCode.SelectedValue = strCategoryCode.ToString();
            ed_catCode.DataBind();
        }
        catch (Exception er)
        {
            error.Text += er.Message;
        }
        finally
        {
            oracleConn.Close();
        }
    }

Answer : SelectedValue which is invalid because it does not exist in the list of items

Now from what I understood from above below is some changed code.
The trick is to check if item exists in your dropdownlist and if not then add it. Then you can set it as selected.

if (ordr.Read())
                {
                   ........
               string  casetype = ordr["CASE_TYPE"].ToString();
               ListItem item = ed_caseType.Items.FindByValue(casetype);
                if (item == null)
              {
                 ed_caseType.Items.Add(new ListItem(casetype, casetype));
              }
       
                    ed_caseType.SelectedValue = casetype;
                    .......
              }
 Hope there are no other side-effects.
Random Solutions  
 
programming4us programming4us