This is a migrated thread and some comments may be shown as answers.

Unhandled ArgumentException when clearing content of DropDownList

3 Answers 116 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Georg
Top achievements
Rank 1
Georg asked on 08 Sep 2011, 02:10 PM
Hi,

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'.
  Source=System
  StackTrace:
       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?

3 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 08 Sep 2011, 03:48 PM
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,
Peter
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 >>

0
Muhammad
Top achievements
Rank 1
answered on 08 Nov 2016, 03:39 PM

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 ??

Regards

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Nov 2016, 09:51 AM
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.

Regards,
Dess
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.
Tags
DropDownList
Asked by
Georg
Top achievements
Rank 1
Answers by
Peter
Telerik team
Muhammad
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or