11

I hope someone can help me solve a little mystery....

This code is in production, and working there. The issue is occuring on my localhost (I'm testing a change I made before I release to staging). This was working until 2 days ago and I have no idea why it would spontaneously stop working.

(This is a .NET 3.5 Web Forms app)

I have a list of "Organizations" that may have reported an incident. The list is populated from the Organization table in my database.

<asp:DropDownList ID="Organizations" runat="server" Width="205" AutoPostBack="True" DataTextField="Name" DataValueField="Id"></asp:DropDownList>

Here's the code to bind the list:

Organizations.DataSource = _service.ListOrganizations()
        .Where(o => o.IsDeleted == false && o.ReportedBy == true)
        .OrderBy(o => 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);

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.

Organizations.SelectedIndex = Organizations.Items.IndexOf(Organizations.Items.FindByValue(organizationId));

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.

I broke this line up more to pinpoint which part is throwing the exception.

ListItem li = Organizations.Items.FindByValue(organizationId.Trim());
int idx = Organizations.Items.IndexOf(li);

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?

Screen Shots

Breakpoint: view organizationId

Here you can see the ListItem we're trying to select. It does exist in the Items collection.

Breakpoint: view ListItem in Items collection, it does exist

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.

All advice/ideas are welcome. Thank you in advance for any assistance.

EDITS

(1) "Can you tell exactly which exception is thrown?" ...Here is the exception detail The exception detail

(2) Property or indexer 'System.Web.UI.WebControls.ListControl.SelectedItem' cannot be assigned to -- it is read only

Organizations.SelectedItem = Organizations.Items.FindByValue(organizationId);

(3) I get the same result if I modify the code to this (below)...it throws the same exception

ListItem li = Organizations.Items.FindByValue(organizationId);

(4) "Did you try to parse it to int did you try to check it's length?" organizationId

(5) Here's the matching ListItem ListItem

(6) Just modified the code to Organizations.Items.FindByValue(organizationId).Selected = true; The exception is thrown on that line now. I rebooted my machine just for giggles, that also had no effect.

CDR12
  • 481
  • 6
  • 21
  • FindByValue shouldn't throw an exception, it should just return `null`. Can you tell exactly which exception is thrown? – freefaller Nov 14 '13 at 18:11
  • 2
    To @freefaller's point, it could be that the internals of IndexOf are throwing a `NullReferenceException` with a `null` argument. You could test that by separating the IndexOf and FindByValue calls. – neontapir Nov 14 '13 at 18:13
  • @freefaller I added a screenshot of the exception detail – CDR12 Nov 14 '13 at 18:18
  • What happens if you just use `Organizations.SelectedItem = Organizations.Items.FindByValue(organizationId);` instead of working out the index and setting the `SelectedIndex`? – freefaller Nov 14 '13 at 18:18
  • @neontapir I have already separated the IndexOf and FindByValue calls, as mentioned in my post. – CDR12 Nov 14 '13 at 18:19
  • That exception screen shot isn't really going to help, other than telling me that something in the code is null when it shouldn't be... without the ability to step through your code with your data, we can't really help – freefaller Nov 14 '13 at 18:20
  • Sorry, CDR, I was sure you could set the `SelectedItem`! When you step through the separated lines, is the `li` valid or `null`? – freefaller Nov 14 '13 at 18:25
  • @freefaller, the exception is thrown during execution of `ListItem li = Organizations.Items.FindByValue(organizationId.Trim());` I never get to see the result – CDR12 Nov 14 '13 at 18:33
  • You have an absolute doozie there mate, I have absolutely no idea what on earth is going on. Final try... what happens if you remove the `.Trim()` and just do a straight `FindByValue(organisationId);`? – freefaller Nov 14 '13 at 18:36
  • Really stupid question, but is either `Organizations` or `Organizations.Items` set to `null` during step-through? If not, then I'm at a total loss and will have to throw the towel in! – freefaller Nov 14 '13 at 18:52
  • That was my first thought...that `Organizations` was `null`...but I can expand the `Items` collection (it contains 87 items) when I hit the breakpoint on `ListItem li = Organizations.Items.FindByValue(organizationId);` and the exception is thrown when I press F10 to step to the next line. – CDR12 Nov 14 '13 at 19:03
  • @CDR12 - can you show the same output as in (4) for the matching item in the list – Yosi Dahari Nov 14 '13 at 19:07
  • I will keep editing my post with the results if your suggestions to limit comments – CDR12 Nov 14 '13 at 19:26
  • You can see this answer- http://stackoverflow.com/a/10974995/2628137 where someone mentioned that solution like yours didn't work for him, give it a try `dropdownlist.SelectedValue = value` and you can ask him why.. – Yosi Dahari Nov 14 '13 at 19:28

1 Answers1

6

Ok, after wasting a day and a half chasing my tail on this...here's the resolution...

I knew it would end up being something dumb...

So I reached a certain level of desperation that led me to start inspecting the 87 ListItems in the Items collection individually. Two of the ListItems were null (??) so that explains the NullReferenceException that seemed so out of place here. Once I made a change to remove the null items from the list the original code worked again.

Organizations.SelectedIndex = Organizations.Items.IndexOf(Organizations.Items.FindByValue(organizationId));

Thank you to all who took time to help me troubleshoot this!

CDR12
  • 481
  • 6
  • 21