Note that there are some explanatory texts on larger screens.

plurals
  1. PODataSource containing a null value makes ComboBox fail
    text
    copied!<p>I've thrown myself headfirst into C# and .Net 2.0 using Linq, and I'm having a few problems debugging some of the problems, namely the following:</p> <p>I have a ComboBox control (<code>cmbObjects</code>) I want to populate with a set of objects retrieved using Linq. I've written a helper method to populate a <code>List&lt;T&gt;</code> generic:</p> <pre><code>class ObjectProvider { public static List&lt;T&gt; Get&lt;T&gt;(bool includeNull) where T : class, new() { List&lt;T&gt; list = new List&lt;T&gt;(); LutkeDataClassesDataContext db = ConnectionManager.GetConnection(); IQueryable&lt;T&gt; objects = db.GetTable&lt;T&gt;().AsQueryable(); if (includeNull) list.Add(null); foreach (T o in objects) list.Add(o); return list; } public static List&lt;T&gt; Get&lt;T&gt;() where T : class, new() { return Get&lt;T&gt;(false); } } </code></pre> <p>I verified the results when calling the function with true or false - the <code>List</code> does contain the right values, when passing <code>true</code>, it contains <code>null</code> as the first value, followed by the other objects.</p> <p>When I assign the <code>DataSource</code> to the <code>ComboBox</code> however, the control simply refuses to display any items, including the <code>null</code> value (not selectable):</p> <pre><code>cmbObjects.DataSource = ObjectProvider.Get&lt;Car&gt;(true); </code></pre> <p>Passing in <code>false</code> (or no parameter) does work - it displays all of the objects.</p> <p>Is there a way for me to specify a "null" value for the first object without resorting to magic number objects (like having a bogus entry in the DB just to designate a N/A value)? Something along the lines of a nullable would be ideal, but I'm kind of lost.</p> <p>Also, I've tried adding <code>new T()</code> instead of <code>null</code> to the list, but that only resulted in an <code>OutOfMemoryException</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload