0

This falls over on the first elseif. Basically I have a load of dropdowns and I'm trying to work out which filters to select based on if people have selected the dropdown items or not.

enter image description here

An unhandled exception of type 'System.NullReferenceException' occurred in 

Based On

 private void btnSearch_Click(object sender, EventArgs e)
    {
        if (ddCompany.SelectedItem.ToString() == null && ddStatus.SelectedItem.ToString() == null)
        {
            UpdateTicketsList("NO", "NO");
        }
        else if (ddCompany.SelectedItem.ToString() != null && ddStatus.SelectedItem.ToString() == null)
        {
            UpdateTicketsList(ddCompany.SelectedItem.ToString(), "NO");
        }
        else if (ddCompany.SelectedItem.ToString() == null && ddStatus.SelectedItem.ToString() != null)
        {
            UpdateTicketsList("NO", ddStatus.SelectedItem.ToString());
        }
    } 
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

1 Answers1

2

Calling ToString on a null object will result in a NullReferenceException.

Remove the calls to ToString in your if-statement expression and your code should work as expected :)

User 12345678
  • 7,714
  • 2
  • 28
  • 46