locked
DropDownList has a SelectedValue which is invalid because it does not exist in RRS feed

  • Question

  • User-1506965535 posted

    When I update the data into the field of dropdownlist, I get error as 

    DropDownList' has a SelectedValue which is invalid because it does not exist in.

    here is the code:-

    public void FunFillDetails()
        {
            string ObjProMkey = TxtMKey.Value;
            DataAccessLayer ObjProdat2 = new DataAccessLayer(Providers.ConfigDefined);
    
            //DbDataReader ObjProDr = ObjProdat2.ExecuteReader("select isnull(SubDept_mkey,0) as SubDept_mkey1,* from emp_mst where mkey =" + ObjProMkey);
            DbDataReader ObjProDr = ObjProdat2.ExecuteReader("Select isnull(mkey,0) as Mkey,* from p_emp_mst_self where mkey=" + ObjProMkey);
    
    
            while (ObjProDr.Read())
            {
                strPubMkey = ObjProDr["mkey"].ToString();
                ddlTitle.SelectedValue = ObjProDr["title"].ToString();
                txtTitle.Text = ObjProDr["title_other"].ToString();
                ddlPrefix.SelectedValue = ObjProDr["prefix"].ToString();
                txtPrefix.Text = ObjProDr["prefix_other"].ToString();
                txtEmpCode.Text = ObjProDr["emp_code"].ToString();
                txt_emp_fname.Text = ObjProDr["emp_f_name"].ToString();
                txt_emp_mname.Text = ObjProDr["emp_m_name"].ToString();
                txt_emp_lname.Text = ObjProDr["emp_l_name"].ToString();
                txtDateofJoin.Text = ObjProDr["date_of_join"].ToString();
           }
    ObjProDr.Close();
    }

    Thursday, July 16, 2015 8:56 AM

Answers

  • User1577371250 posted

    Hi,

    This line is failing.

                ddlPrefix.SelectedValue = ObjProDr["prefix"].ToString();
    

     Change as below and try

    if (ddlPrefix.Items.FindByValue(ObjProDr["prefix"].ToString()) != null)
    {
        ddlPrefix.SelectedValue = ObjProDr["prefix"].ToString();
    }
    else
    {
    // set as the index to 0
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 9:21 AM
  • User2103319870 posted

    When I update the data into the field of dropdownlist, I get error as 

    DropDownList' has a SelectedValue which is invalid because it does not exist in.

    You could also use the FindByText method and then set the value in dropdownlist

                //Check if the value database is available in dropdownlist
                if (this.ddlPrefix.Items.FindByText(ObjProDr["prefix"].ToString()) != null)
                {
                    //if so then set the particular value as selected
                    this.ddlPrefix.Items.FindByText(ObjProDr["prefix"].ToString()).selected = true;
    
                }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 7:15 PM

All replies

  • User1577371250 posted

    Hi,

    This line is failing.

                ddlPrefix.SelectedValue = ObjProDr["prefix"].ToString();
    

     Change as below and try

    if (ddlPrefix.Items.FindByValue(ObjProDr["prefix"].ToString()) != null)
    {
        ddlPrefix.SelectedValue = ObjProDr["prefix"].ToString();
    }
    else
    {
    // set as the index to 0
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 9:21 AM
  • User2103319870 posted

    When I update the data into the field of dropdownlist, I get error as 

    DropDownList' has a SelectedValue which is invalid because it does not exist in.

    You could also use the FindByText method and then set the value in dropdownlist

                //Check if the value database is available in dropdownlist
                if (this.ddlPrefix.Items.FindByText(ObjProDr["prefix"].ToString()) != null)
                {
                    //if so then set the particular value as selected
                    this.ddlPrefix.Items.FindByText(ObjProDr["prefix"].ToString()).selected = true;
    
                }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 7:15 PM
  • User-1506965535 posted

    Yes, now that's working. But I am getting new error as

    Specified argument was out of the range of valid values.Parameter name: index

    at line 

    xw.WriteElementString("degree", GrdQualification.Rows[intCount].Cells[GrdQualification.Columns.GetColumnIndexByDataField("Degree")].Text.Trim());

    Friday, July 17, 2015 1:14 AM
  • User-271186128 posted

    Hi Nadeem157,

    But I am getting new error as

    Specified argument was out of the range of valid values.Parameter name: index

    at line 

    xw.WriteElementString("degree", GrdQualification.Rows[intCount].Cells[GrdQualification.Columns.GetColumnIndexByDataField("Degree")].Text.Trim());

    As for this error, since it is another question, I suggest you could post a new thread. And, in the new thread, I suggest you could post the error message with some relevant code, it might be easier for us to guide you and help you get what you need.

    Best Regards,
    Dillion

    Sunday, July 26, 2015 9:58 PM