How to populate a string based on whether a list box item was selected?

What is wrong with the following if statement to check the value of a ListBox to determine if a choice was made?

$exception      {"Object reference not set to an instance of an object."}

      if (listBox5.ToString().Equals(null))
            {
                branchnum = "";
            }
            else
            {
                branchnum = listBox5.SelectedValue.ToString();
            }
zimmer9Asked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like:
            if (listBox5.SelectedValue == null)
            {
                branchnum = ""; 
            }
            else
            {
                branchnum = listBox5.SelectedValue.ToString(); 
            }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.