Help with Try... Catch and the Selected Value of a DropDownList

Posted on 2012-08-17
Last Modified: 2012-08-20
Hi Experts - I need help using the Try... catch block with my DropDownList Control.

The value of the drop down have recently changed and may no longer match the values in my database.  That being the case i wanted to set the SelectedValue for any non-matched items to the default value of "Unassigned".  Here's what I wrote:

'  Get the values for the dropdownlist
' -------------------------------------------------------------------------------
Dim MyBLL As New PickPackBLL()
Dim dt As DataTable = MyBLL.GetBinLocs()

ddOpp.DataSource = dt
ddOpp.DataTextField = "BinLoc"
ddOpp.DataValueField = "BinLoc"

'  Set the Selected Value of the drop down list.
' -------------------------------------------------------------------------------
Dim SelectedBinLoc As String
Dim defaultSelectedIndex As String = "Unassigned"

Dim pickTicketLogic As New PickPackItemsBLL()
Dim dtpick As DataTable = pickTicketLogic.GetPickTicketItems(hfPickTicketID.Value)

If dtpick.Rows.Count > 0 Then
    Dim row As DataRow = dtpick.Rows(0)
    SelectedBinLoc = row("BinLoc")
End If

    ddOpp.SelectedValue = SelectedBinLoc
Catch ex As Exception
    ddOpp.SelectedValue = defaultSelectedIndex
End Try


Open in new window

Even with the "Try" block in place I'm still getting the "'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items."

I was hoping that the try would catch that error and the move on to the exception block and useing the defaultSelectedIndex value.

Can you tell me what I'm doing wrong?

Question by:cdemott33
    LVL 35

    Assisted Solution

    by:Miguel Oz
    Replace try code with:
    If Not ddOpp.Items.FindByValue(SelectedBinLoc) Is Nothing Then 
        ddOpp.SelectedValue = defaultSelectedIndex     ' do what the Exception is supposed to do ' 
        DropDownList.SelectedValue = SelectedBinLoc
    End If 

    Open in new window

    For more details Check:
    LVL 40

    Assisted Solution

    by:Jacques Bourgeois (James Burger)
    mas_oz2003 points you in the right direction. I would simply like to expand "in theory" on what he is pointing to.

    Using a Try...Catch is for things on which you have no control. A Try...Catch has a lot of work to do in order to get information about the error.

    Whenever an If can do the job, it is the preferred method. First of all, you can explicitely define the condition that you do not accept, while you have no such control over when an exception will fire. Second, and if requires a lot less resources, which is always a good goal to achieve, even in these days where too many programmers seem to think that memory is limitless.
    LVL 26

    Accepted Solution

    Hi cdemott33,
    wondering if you need to databind the dropdownlist to the datatable after setting it's DataSource, DataTextField and DataValueField; before setting it's selectedvalue ?

    Open in new window


    Author Closing Comment

    Thanks everyone for your help.  I had two problems.  One was the use of the try block. and the other was the databind.  Once I moved the databind above the code where I set the SelectedValue everything worked.

    Thanks again!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now