Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / May 2006

Tip: Looking for answers? Try searching our database.

Silly OutOfMemoryException thrown from databound ComboBox...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Baulch - 24 Apr 2006 09:33 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".

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.