Note that there are some explanatory texts on larger screens.

plurals
  1. PONullReferenceException on DropDownList.Items.FindByValue()
    primarykey
    data
    text
    <p>I hope someone can help me solve a little mystery....</p> <p>This code is in production, and working there. The issue is occuring on my localhost <em>(I'm testing a change I made before I release to staging).</em> This was working until 2 days ago and I have no idea why it would spontaneously stop working.</p> <p>(This is a .NET 3.5 Web Forms app)</p> <p>I have a list of "Organizations" that may have reported an incident. The list is populated from the Organization table in my database.</p> <pre><code>&lt;asp:DropDownList ID="Organizations" runat="server" Width="205" AutoPostBack="True" DataTextField="Name" DataValueField="Id"&gt;&lt;/asp:DropDownList&gt; </code></pre> <p>Here's the code to bind the list:</p> <pre><code>Organizations.DataSource = _service.ListOrganizations() .Where(o =&gt; o.IsDeleted == false &amp;&amp; o.ReportedBy == true) .OrderBy(o =&gt; o.Name); Organizations.DataBind(); Organizations.Items.Insert(0, new ListItem("Please make a selection", "-1")); // Find the "VICTIM...." items ListItem victim = Organizations.Items.FindByText("VICTIM"); ListItem guardian = Organizations.Items.FindByText("VICTIM'S PARENT/GUARDIAN"); ListItem child = Organizations.Items.FindByText("VICTIM'S SON/DAUGHTER"); ListItem partner = Organizations.Items.FindByText("VICTIM'S SPOUSE/DOMESTIC PARTNER"); ListItem unknown = Organizations.Items.FindByText("UNKNOWN"); // Move the "VICTIM...." items to the top of the list, under the default item Organizations.Items.Remove(victim); Organizations.Items.Remove(child); Organizations.Items.Remove(guardian); Organizations.Items.Remove(partner); Organizations.Items.Remove(unknown); Organizations.Items.Insert(1, victim); Organizations.Items.Insert(2, guardian); Organizations.Items.Insert(3, child); Organizations.Items.Insert(4, partner); Organizations.Items.Insert(5, unknown); </code></pre> <p>When I click on the "edit" icon to view/edit the details of a Case and my app tries to populate the form I get a NullReferenceException when it tries to set the SelectedIndex of the Organizations list.</p> <pre><code>Organizations.SelectedIndex = Organizations.Items.IndexOf(Organizations.Items.FindByValue(organizationId)); </code></pre> <p>If I set a breakpoint on this line (above) I can expand the Items collection and see that it does contain valid data and I can even locate the ListItem that matches the organizationId we are looking for. However, as soon as I hit F10 an exception is thrown. </p> <p>I broke this line up more to pinpoint which part is throwing the exception.</p> <pre><code>ListItem li = Organizations.Items.FindByValue(organizationId.Trim()); int idx = Organizations.Items.IndexOf(li); </code></pre> <p>I called Trim() on the organizationId just in case there were any white spaces that shouldn't be there. Organizations.Items.FindByValue(organizationId.Trim()); throws the exception. This makes no sense to me. If the item is in the list, why is it not found?</p> <p><strong>Screen Shots</strong></p> <p><img src="https://i.stack.imgur.com/OKJ9u.jpg" alt="Breakpoint: view organizationId"></p> <p>Here you can see the ListItem we're trying to select. It does exist in the Items collection.</p> <p><img src="https://i.stack.imgur.com/j98im.jpg" alt="Breakpoint: view ListItem in Items collection, it does exist"></p> <p>I thought maybe it was happening for just one Case, but it's not. I have attempted to edit multiple Cases and the same thing happens when the form is populated, no matter which Case I tried to edit.</p> <p>All advice/ideas are welcome. Thank you in advance for any assistance. </p> <h2>EDITS</h2> <p>(1) <em>"Can you tell exactly which exception is thrown?"</em> ...Here is the exception detail <img src="https://i.stack.imgur.com/qZtj8.jpg" alt="The exception detail"></p> <p>(2) Property or indexer 'System.Web.UI.WebControls.ListControl.SelectedItem' cannot be assigned to -- it is read only</p> <pre><code>Organizations.SelectedItem = Organizations.Items.FindByValue(organizationId); </code></pre> <p>(3) I get the same result if I modify the code to this (below)...it throws the same exception</p> <pre><code>ListItem li = Organizations.Items.FindByValue(organizationId); </code></pre> <p>(4) <em>"Did you try to parse it to int did you try to check it's length?"</em> <img src="https://i.stack.imgur.com/hglIB.jpg" alt="organizationId"></p> <p>(5) Here's the matching ListItem <img src="https://i.stack.imgur.com/0eDYD.jpg" alt="ListItem"></p> <p>(6) Just modified the code to <code>Organizations.Items.FindByValue(organizationId).Selected = true;</code> The exception is thrown on that line now. I rebooted my machine just for giggles, that also had no effect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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