I just waisted so much time on the following issue that I decided to post it to the groups for permanent reference for anybody else who comes across it.
When databinding a DropDownList ComboBox to a list of business objects via a BindingSource, make sure that if you've overridden ToString() that it will never return null.
If it does, you may receive an OutOfMemoryException once your form is made visible stating that there are "too many items in the combo box".
To replicate, create a form and add a ComboBox and BindingSource. Then paste in the following code in the appropriate places.
public partial class Form1 : Form
{
public TestForm()
{
InitializeComponent();
comboBox1.DataSource = bindingSource1;
bindingSource1.DataSource = new List<Fubar>(new Fubar[]
{
new Fubar(),
new Fubar(),
new Fubar()
});
}
class Fubar
{
public override string ToString()
{
return null;
}
}
}
The following is the full exception stack trace.
System.OutOfMemoryException was unhandled
Message="Too many items in the combo box."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.ComboBox.NativeAdd(Object item)
at System.Windows.Forms.ComboBox.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.ComboBox.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at AgLab.Farmetrics.Program.Main() in C:\test\Farmetrics\Program.cs:line 15
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Nathan
js_dev@rediffmail.com - 03 May 2006 13:46 GMT
> I just waisted so much time on the following issue that I decided to post it to the groups for permanent reference for anybody else who comes across it.
>
> When databinding a DropDownList ComboBox to a list of business objects via a BindingSource, make sure that if you've overridden ToString() that it will never return null.
> If it does, you may receive an OutOfMemoryException once your form is made visible stating that there are "too many items in the combo box".
Thanks, Nathan.
I got the same error in the same situation.
Just for the sake of completion of the discussion, any idea why should
it show _OutOfMemory_Exception? and that too with a "too many items in
the combo box" when actually the single string in the single Item
concerned is null? Is it a bug worth reporting?
The standard help that ships with the SDK has no mention of such a
peculiarity.
Regards,
JS