Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown list databinding error

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have a dropdown list within a template field in details view

Code:
<asp:TemplateField HeaderText="Allocate Babysitter">
                             <EditItemTemplate>
                             <asp:DropDownList ID="ddlBabysitter" runat="server" SelectedValue='<%# Bind("BabysitterID") %>'  
                                DataSourceID="GetBabysittersList" DataTextField="BabysitterName" 
                                DataValueField="BabysitterID" ondatabinding="PreventErrorOnbinding" >
                                <asp:ListItem Text="none" Value=""></asp:ListItem>
                            </asp:DropDownList>    
                             </EditItemTemplate>
                             <ItemTemplate>
                             <asp:Label ID="lblBabysitter" runat="server" Text='<%# Bind("BabysitterName")%>'></asp:Label>
                             </ItemTemplate>
                             </asp:TemplateField>

I am getting the following error whenever the page loads:
Code:
ddlBabysitter' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I read on a website to add the following code to my codebehind which would eliminate the error

Code:
 Protected Sub PreventErrorOnbinding(sender As Object, e As EventArgs)
        Dim ddlBabysitter As DropDownList = DirectCast(sender, DropDownList)
        ddlBabysitter.DataBinding -= New EventHandler(AddressOf PreventErrorOnbinding)
        ddlBabysitter.AppendDataBoundItems = True
        Dim li As New ListItem("Make a selection >>", "")
        ddlBabysitter.Items.Insert(0, li)
        Try
            ddlBabysitter.DataBind()
        Catch generatedExceptionName As ArgumentOutOfRangeException
            ddlBabysitter.SelectedValue = ""
        End Try
    End Sub

But now I get the following error:

Code:
'Public Event DataBinding(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
on the line
Code:
ddlBabysitter.DataBinding -= New EventHandler(AddressOf PreventErrorOnbinding)
 
First, I've never seen code like that, I can't imagine that would work, and it doesn't based on your error.
Second, stop using the DataSource controls. They are terrible for many reasons, one being that you cannot debug them or any code within them.
Third, you should never have a possible value coming back from the DB that is not present in your DDL. The DDL should contain any possible values.

Lastly, you need to add your event handler like this:
AddHandler ddlBabysitter.DataBinding, AddressOf PreventErrorOnbinding
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top