0

In the below code i got a invalid cast exception was handled by user code (ie)Unable to cast object of type 'System.Web.UI.WebControls.DataList' to type 'usercontrol.Fields' at throw ex. Please help me to solve the issue. Field.ascx

 protected void cbField_Dependent(object sender, EventArgs e)
        {
            try
            {
                if (this.ParentFieldInfo != string.Empty)
                {
                    Fields gForm = (Fields)this.Parent.Parent;
                    string strParentVal = gForm.GetParentSelection(ParentFieldInfo);
                    int NoOfItemsAdded = 0;
                    bool bDefaultSelection = false;
                    this.cbField.Items.Clear();
                    for (int iRow = 0; iRow < EnumRows.Length; iRow++)
                    {
                        if (EnumRows[iRow]["DependencyEnumText"].ToString().Equals(strParentVal) || EnumRows[iRow]["DependencyEnumText"].ToString().Equals(string.Empty))
                        {
                            this.cbField.Items.Add(EnumRows[iRow]["EnumText"].ToString());
                            if (Boolean.Parse(EnumRows[iRow]["DefaultSelection"].ToString()))
                            {
                                this.cbField.SelectedIndex = NoOfItemsAdded;
                                bDefaultSelection = true;
                                if (this.strValue == null || this.strValue == string.Empty)
                                    this.strValue = cbField.Items[iRow].ToString();
                            }
                            NoOfItemsAdded++;
                        }
                    }

                    if (!bDefaultSelection && this.cbField.Items.Count > 0)
                        this.cbField.SelectedIndex = 0;

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

feilds.ascx

public string GetParentSelection(string strFieldName)
        {
            try
            {
                for (int iItems = 0; iItems < this.Items.Count; iItems++)
                {
                    if (this.Items[iItems].FieldLabel == strFieldName)
                    {
                        return this.Items[iItems].strValue;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return string.Empty;
        }
  • 2
    On which line you get this exception? – Soner Gönül Feb 12 '14 at 06:58
  • 1
    You catch an exception, that you throw it again - what's the point ? – Aristos Feb 12 '14 at 07:00
  • In field.ascx (throw ex)line i debug it on this line Fields gForm = (Fields)this.Parent.Parent; and it throws – user3278874 Feb 12 '14 at 07:03
  • 2
    [Don't do `throw ex;`, just do `throw;`](http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex) otherwise you loose the stack trace and it will point at the `throw ex;` line instead of the real line the error happens on. – Scott Chamberlain Feb 12 '14 at 07:17
  • In addition to Scott Chamberlain's comment: Your catch blocks are meaningless; catching an exception to only rethrow it does not anything useful. Therefore I'd remove the try-catch-blocks altogether. This will also give you a better insight on where the exception is thrown. – Markus Feb 12 '14 at 08:31

0 Answers0