Gridview EditItemTemplate DropDownList Binding Issue in ASP.net 2.0

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

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


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

PS. THERE ARE NO NULL VALUES RETURNED IN THE DATASOURCE
LVL 1
Artform04Asked:
Who is Participating?
 
PHDCommented:

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 ?
0
 
PHDCommented:
Are you sure that all value in the database are included in the values of the dropdownlist ?
0
 
Artform04Author Commented:
well, using the visualiser for the datasource in visual studio 2005, i tested the query and all the data came out perfect...?
0
 
Artform04Author Commented:
I have solved the problem, nothing to do with read mode, but congrats... as u tried, you get 500 points!!
0
 
PHDCommented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.