Dropdownlist SelectedValue does not throw error

382 Views Asked by At

I have run into a strange issue dealing with the SelectedValue property of a dropdown list. We were incorrectly setting the SelectedValue property to an invalid selection (zero), but we were not aware of this issue because the exception is not being thrown in development. In development, the code below executes with no problem, but in production it throws an error.

int val = GetValue(); // Note: The GetValue() method is incorrectly returning a 0
ddlDropdown.SelectedValue = val.ToString(); // This line should throw an error

There is no item with a value of 0 in the dropdown, in either the production or dev environments. However, in dev, when this code executes, it simply does nothing. The ddlDropdown.SelectedValue property is a "1" before the line executes and it remains a "1" after the line executes (even though the code is trying to set it to 0). But in production, an error is being logged every time ("ddlDropdown has a SelectedValue which is invalid..."). I've even tried setting the value of the dropdown in the immediate window - setting it to a valid value (a "3", for example) works, but setting it to "0" has the same result - it simply does nothing.

I've researched the error being thrown and I've found references to the error being thrown only during a postback, and not on initial page load. But this code is only called in places that are wrapped in a if (!IsPostBack) so it should only be called on initial page load.

I've fixed the code to no longer incorrectly set the value to 0, but I cannot verify that the fix will work in production since I can't replicate the error in my dev environment.

Can anyone figure out why this code would be throwing an error in production and not in development?

0

There are 0 best solutions below