SYSTEM NULLREFERENCEEXCEPTION DROPDOWNLIST

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
C# 
C#
 1477  View(s)
Ratings:
Rate this:

function abc(){

     string Code = (combobox.SelectedItem.Value != null) ? combobox.SelectedItem.Value.ToString() : string.Empty;

     (save function ....)

}

Anyone knows how to I can proceed with save function if the combobox is null value and it pop up error system null reference exception?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: system nullreferenceexception dropdownlist

    Hi Jeet,

    You didn't post where you get the NullReferenceException, however it's most likely when you are performing this evaluation

    combobox.SelectedItem.Value != null

    The ComboBox's SelectedItem property will return null if nothing is selected. If you call the Value property on the null selected item, you'll get a NullReferenceException. What you should be checking is:

    combobox.SelectedItem != null

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!