locked
Error - Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.DropDownList'. RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I am getting above error on below line

    DropDownList ddl_NMSMonths0 = (DropDownList)row.FindControl("lblNMSMonths");

    Thanks

    Tuesday, August 14, 2018 5:46 PM

Answers

  • User475983607 posted

    I want label value to be displayed as selected value for dropdown in Update

    JafjitSingh, that's is not what the code - you wrote - is doing.   Your code is trying to convert a label to a dropdown.  

    Use the selected value property of the dropdown list to set the selected value.  This syntax assumes you have a ddl_NMSMonths0 dropdown defined in the markup.

    ddl_NMSMonths0.SelectedValue = ((Label)row.FindControl("lblNMSMonths")).Text;

    I'm not sure how to help you.... Keep in mind that you regularly  ask the same questions and make the same mistakes. Maybe you should slow down and do a bit of learning.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 14, 2018 6:30 PM

All replies

  • User475983607 posted

    DropDownList ddl_NMSMonths0 = (DropDownList)row.FindControl("lblNMSMonths");

    The error is very clear.  You are trying to cast a label, lblNMSMonths, into a DropDown control.

    Please take a few seconds to review your code before posting!  

    Tuesday, August 14, 2018 6:03 PM
  • User-1499457942 posted

    Hi

       I want label value to be displayed as selected value for dropdown in Update

    Thanks

    Tuesday, August 14, 2018 6:04 PM
  • User475983607 posted

    I want label value to be displayed as selected value for dropdown in Update

    JafjitSingh, that's is not what the code - you wrote - is doing.   Your code is trying to convert a label to a dropdown.  

    Use the selected value property of the dropdown list to set the selected value.  This syntax assumes you have a ddl_NMSMonths0 dropdown defined in the markup.

    ddl_NMSMonths0.SelectedValue = ((Label)row.FindControl("lblNMSMonths")).Text;

    I'm not sure how to help you.... Keep in mind that you regularly  ask the same questions and make the same mistakes. Maybe you should slow down and do a bit of learning.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 14, 2018 6:30 PM