?
Solved

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

Posted on 2012-08-17
4
Medium Priority
?
1,186 Views
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

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

ddOpp.DataBind()

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?

Thanks!
0
Comment
Question by:cdemott33
4 Comments
 
LVL 36

Assisted Solution

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

Open in new window

For more details Check:
http://stackoverflow.com/questions/2132527/asp-net-setting-selectedvalue-that-doesnt-belong-to-the-list-does-not-trigger-e
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 668 total points
ID: 38307121
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.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 668 total points
ID: 38307628
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 ?
ddOpp.DataBind()

Open in new window

Alan
0
 

Author Closing Comment

by:cdemott33
ID: 38313173
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!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

862 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