By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,926 Members | 1,828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,926 IT Pros & Developers. It's quick & easy.

Check if item is selected in listbox

P: n/a
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(lbSpecificProgr am.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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
martin wrote:
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(lbSpecificProgr am.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.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #2

P: n/a
check for (ListBox1.SelectedIndex != -1)
Av.
"martin" <St**************@yahoo.com> wrote in message
news:%2********************@TK2MSFTNGP10.phx.gbl.. .
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(lbSpecificProgr am.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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.