Avoiding using Try & Catch when setting drop down list selected value

A better way to avoid errors when setting the selected item of a drop down list which avoids using a Try and Catch statment is to change:

Try
        
Me.ddlCountry.Items.FindByValue(Profile.Customer.CountryID).Selected = True
Catch ex As Exception
         
' Select the default country
        
Me.ddlCountry.Items(1).Selected = True
End Try

To this:

Dim ddlItem As ListItem
ddlItem = ddlCountry.Items.FindByValue(Profile.Customer.CountryID)

If Not ddlItem Is Nothing Then
         
ddlItem.Selected = True
Else
        
' Set a default
End If

Thanks to Eric Wise for that! :0)

Published Saturday, February 09, 2008 10:20 AM by scott@elbandit.co.uk

Comments

# re: Avoiding using Try & Catch when setting drop down list selected value

Saturday, February 09, 2008 8:19 AM by Anatoly

       public static void SetComboValue(ListControl Combo, string Value)

       {

           Combo.SelectedIndex = Combo.Items.IndexOf(Combo.Items.FindByValue(Value));

       }

Leave a Comment

(required) 
(required) 
(optional)
(required)