Gridview EditItemTemplate DropDownList Binding Issue in 2.0

Posted on 2007-07-25
Last Modified: 2013-11-07
PROBLEM: I've creating an editItemTemplate within a gridview control. In this template, i have made a DDL which is populated from the datasource i created for that control. This datasource has two fields... the Name and the ID. I have tried to set the text on the DDL to be the name and the SelectedValue to be the ID. When I run the page i ge tthe following:

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsUnitTypeAndId"
                            DataTextField="Name" DataValueField="UnitTypeId" SelectedValue='<%# Bind("UnitType") %>'>
                        <asp:SqlDataSource ID="dsUnitTypeAndId" runat="server" ConnectionString="<%$ ConnectionStrings:SmartEstateManagementConnectionString %>"
                            SelectCommand="SELECT [UnitTypeId], [Name] FROM [tblUnitType]">

I have tried different combo's of the UnitTypeId And Name being assigned to the DATAVALUEFIELD AND DATATEXTFIELD. Everything works except the combination I need which is the Id as the VALUE and the Name as the TEXT.

If anyone has any suggestions, your help would be much appreciated.

Thanks, Phil

Question by:Artform04
    LVL 6

    Expert Comment

    Are you sure that all value in the database are included in the values of the dropdownlist ?
    LVL 1

    Author Comment

    well, using the visualiser for the datasource in visual studio 2005, i tested the query and all the data came out perfect...?
    LVL 6

    Accepted Solution


    If you query the database with this select : select distinct UnitTypeId from tblUnitType
    All the UnitTypedId of the result are values of the dropdownlist.

    This Just to be sure that the all values UnitTypeId of the datasource match a value in the dropdownlist otherwise the dropdownlist.selectedvalue cannot be bound.

    the error occur when the gridview is in read mode ?
    LVL 1

    Author Comment

    I have solved the problem, nothing to do with read mode, but congrats... as u tried, you get 500 points!!
    LVL 6

    Expert Comment

    thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    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…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now