Unhandled ArgumentException when clearing content of DropDownList

4 posts, 0 answers
  1. Georg
    Georg avatar
    4 posts
    Member since:
    Jul 2011

    Posted 08 Sep 2011 Link to this post


    I am having troubles with the DropDownList. I have bound the "SelectedValue"-property of the DropDownList to an int:
    myDropDownList.DataBindings.Add("SelectedValue", this, "myID", false, DataSourceUpdateMode.OnPropertyChanged);

    When typing something in the DropDownList everything is OK. But I a remove the typed Text I get a very strange exception:

    System.ArgumentException was unhandled
      Message=Object of type 'System.DBNull' cannot be converted to type 'System.Int32'.
           at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
           at System.Windows.Forms.BindToObject.SetValue(Object value)
           at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
           at System.Windows.Forms.Binding.Target_PropertyChanged(Object sender, EventArgs e)
           at Telerik.WinControls.UI.RadDropDownList.OnSelectedValueChanged(Object sender, Int32 newIndex, Object oldValue, Object newValue)
           at Telerik.WinControls.UI.RadDropDownList.element_SelectedValueChanged(Object sender, ValueChangedEventArgs e)
           at Telerik.WinControls.UI.RadDropDownListElement.OnSelectedValueChanged(Object sender, ValueChangedEventArgs e)
           at Telerik.WinControls.UI.RadDropDownListElement.NotifyOwner(PopupEditorNotificationData notificationData)
           at Telerik.WinControls.UI.PopupEditorElement.listElement_SelectedValueChanged(Object sender, EventArgs e)
           at Telerik.WinControls.UI.RadListElement.OnSelectedValueChanged(Int32 newIndex)
           at Telerik.WinControls.UI.RadListElement.OnSelectedIndexChanged(Int32 newIndex)
           at Telerik.WinControls.UI.RadListElement.SetSelectedIndex(Int32 value)
           at Telerik.WinControls.UI.RadDropDownListElement.set_SelectedIndex(Int32 value)
           at Telerik.WinControls.UI.RadDropDownListElement.OnTextChanged(RadDropDownListEditableAreaElement sender, EventArgs args)
           at Telerik.WinControls.UI.RadDropDownListElement.NotifyOwner(PopupEditorNotificationData notificationData)
           at Telerik.WinControls.UI.RadDropDownListEditableAreaElement.textBox_TextChanged(Object sender, EventArgs e)
           at Telerik.WinControls.RadItem.OnTextChanged(EventArgs e)
           at Telerik.WinControls.RadItem.OnPropertyChanged(RadPropertyChangedEventArgs e)
           at Telerik.WinControls.RadObject.RaisePropertyNotifications(RadPropertyValue propVal, Object oldValue, Object newValue, ValueSource oldSource)
           at Telerik.WinControls.RadObject.SetValueCore(RadPropertyValue propVal, Object propModifier, Object newValue, ValueSource source)
           at Telerik.WinControls.RadElement.SetValueCore(RadPropertyValue propVal, Object propModifier, Object newValue, ValueSource source)
           at Telerik.WinControls.RadObject.OnTwoWayBoundPropertyChanged(PropertyBinding binding, Object newValue)
           at Telerik.WinControls.PropertyBinding.UpdateSourceProperty(Object newValue)
           at Telerik.WinControls.RadPropertyValue.SetLocalValue(Object value)
           at Telerik.WinControls.RadObject.SetValueCore(RadPropertyValue propVal, Object propModifier, Object newValue, ValueSource source)
           at Telerik.WinControls.RadElement.SetValueCore(RadPropertyValue propVal, Object propModifier, Object newValue, ValueSource source)
           at Telerik.WinControls.UI.RadTextBoxItem.TextBoxControl_TextChanged(Object sender, EventArgs e)

    Can anybody help?
  2. Peter
    Peter avatar
    1157 posts

    Posted 08 Sep 2011 Link to this post

    Hi Georg,

    Thank you for contacting us.

    This behavior our desired behavior, because the SelectedValue property is synchronized with the Text property. When a user clears the text, then the SelectedValue property is set to null and the SelectedIndex property is set to -1. The simple data-binding algorithm attempts to update the data-bound object myID (of type Int32) with this null value and fails because this object cannot be set to null.

    You can avoid this from happening by setting the DropDownStyle property  to DropDownList. This property determines whether the text area at the top of the control can be edited. The default setting is DropDown and it allows editing. In case when you want to enable the editing you should remove the binding and synchronize the myID field manually. Please consider the code snippet below:

    void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        if (this.radDropDownList1.SelectedIndex > -1)
            this.myID = int.Parse(this.radDropDownList1.SelectedValue.ToString());

    I hope this helps. If you need further assistance I will be glad to help.

    All the best,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. Muhammad
    Muhammad avatar
    10 posts
    Member since:
    Jun 2013

    Posted 08 Nov 2016 in reply to Peter Link to this post

    I am facing the same problem, but what i am suppose to do when i need to reflect current value as per object's which might be changed by some other form or control ??



  4. Dess | Tech Support Engineer, Sr.
    Dess | Tech Support Engineer, Sr.  avatar
    3895 posts

    Posted 09 Nov 2016 Link to this post

    Hello Muhammad,

    Thank you for writing.  

    If you set the DropDownStyle property to DropDownList, the user won't be allowed to clear the value and thus, the exception will be provoked. If the object, used for the simple data binding, implements INotifyPropertyChanged and you change the numeric property outside the RadDropDownList, e.g. in the RadButton.Click event, the RadDropDownList will be updated automatically since it is notified for the update. I have attached a sample project for your reference.

    If you are still experiencing any further difficulties,  Could you please specify the exact steps how to reproduce the problem with the provided sample project? Thus, we would be able to make an adequate analysis of the problem and assist you further. Thank you.

    I hope this information helps. Should you have further questions I would be glad to help.

    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top