RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 129, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


VB2005 BUG: Rebinding a DropDownList with a SelectedValue or SelectedIndex AFTER a PostBack (to an empty dataObject) causes Errors

If you have seen an error like this:

'ddlTest' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value

then you know this can be nasty to figure out why it is happening.  All of the documented research I have been looking at points this to an error that was in VS2005 Beta2, or when you use templates.  I am not doing either.

Here is where the error occurs:

[ArgumentOutOfRangeException: 'ddlTest' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.set_SelectedIndex(Int32 value) +1776990
   System.Web.UI.WebControls.DropDownList.set_SelectedIndex(Int32 value) +4
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +842
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +104
   System.Web.UI.WebControls.ListControl.PerformSelect() +31
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   liveMeetingSurvey.UI.frontPage.DDLBind() in
[EDIT]page.aspx.vb:319
   liveMeetingSurvey.UI.frontPage.Page_Load(Object sender, EventArgs e) in
[EDIT]page.aspx.vb:343
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Here is the code that causes the error:

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            ddlTest.SelectedIndex = 1

            DDLBind()

            ddlTest.SelectedIndex = 1

            DDLBind()

        End Sub

 

        Private Sub DDLBind()

            Dim dt As New DataTable

            With ddlTest

                .DataSource = dt

                .DataTextField = "ProgramName"

                .DataValueField = "ProgramID"

                .DataBind()

                .Items.Insert(0, New ListItem("No Program", "0"))

                .Items.Insert(1, New ListItem("something", "1"))

            End With

        End Sub

 

Add a drop down and a button to click to cause a postback to the page.  Name the drop down ddlTest. Put a breakpoint on the pageLoad event.  Now run through the code. Notice that when you set the selectedIndex to 1 and there are no items in the dropDownList, it continues to be -1. When you add an item to the dropDownList, it sets to a SelectedIndex of 0 (and no longer -1). On postback, it correctly holds the value it should.  When you hit the line of code that causes the dropDownList to rebind, it causes the error described above.

If you think this may be caused by me setting SelectedIndex to 1, change the code to set SelectedIndex to 0 and rerun the application.  I was testing scenarios to try to identify the source of the problem and so I used two list items instead of just one.  You can remove everything that has to do with adding the second listItem, set the SelectedIndex to 0, and you will find that this error still occurs.

This was a known issue in Beta2: http://lab.msdn.microsoft.com/vs2005/get/currentreadme/VSKnownIssues.rtf

Thanks to the following people for having relevant posts to help me with researching the issue: Raducu, this guy, this posting, this posting, and this guy.

 

Update: BUG Report: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=2325fd0b-cc01-4b37-98bf-5d4a6b0509c3&lc=1033

posted on Monday, March 06, 2006 4:00 PM