Check if item is selected in listbox

M

martin

Hi,

I am looking for a method to check if an item (any item) is selected in a
particular listbox before deleteing it from my database, however I keep
getting errors,

The code I have is

lbSpecificProgram is a listbox.

If (Not IsDBNull(lbSpecificProgram.SelectedItem.Value)) Or (Not
lbSpecificProgram.SelectedItem.Value Is Nothing) Then

SpecificProgram.DeleteEntryForSite(lbSpecificProgram.SelectedItem.Value.ToSt
ring)
End If

However, if nothing is selected in the list box then an exception is thrown.
I realize that I could just catch the exception and throw it away but i
would rather use a conditional statement if this is possible.


any help is appreciated

cheers

martin
 
C

Craig Deelsnyder

martin said:
Hi,

I am looking for a method to check if an item (any item) is selected in a
particular listbox before deleteing it from my database, however I keep
getting errors,

The code I have is

lbSpecificProgram is a listbox.

If (Not IsDBNull(lbSpecificProgram.SelectedItem.Value)) Or (Not
lbSpecificProgram.SelectedItem.Value Is Nothing) Then

SpecificProgram.DeleteEntryForSite(lbSpecificProgram.SelectedItem.Value.ToSt
ring)
End If

However, if nothing is selected in the list box then an exception is thrown.
I realize that I could just catch the exception and throw it away but i
would rather use a conditional statement if this is possible.


any help is appreciated

cheers

martin

Switch the two pieces of the Or around and change Or to OrElse.

If (Not lbSpecificProgram.SelectedItem.Value Is Nothing) OrElse (Not
IsDBNull(lbSpecificProgram.SelectedItem.Value)) Then

OrElse stops executing the pieces of the expression as soon as a false
piece is found. So if you rearrange like I said, it would see if
SelectedItem is Nothing, and if it is, skip the if statement rather than
giving you your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top