Gridview Edit has a SelectedValue which is invalid because it does not exist in the list of items.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have been banging my head over this for couple weeks now. I started coding this practice project in VB then yesterday decided to recode this in CSharp to see if the problem still exists which it does. My cascadingdropdownlist throws this error and I cant
for the life of me find anywhere that actually has a concrete solution to this issue. I have tried numerous things that I read and all of them have failed me.
Basically I have a gridview that is populated with my objectdatasource which gets filled with my databasecomponet class. If i was to click Insert on my gridview my cascading dropdownlist work great the values appear as they should I can submit it to the
database and everything works.
Now the problem happens when I want to just edit a record it throws

<div style="color:Black;background-color:White; <pre>
<span style="color:#A31515; ddlgvRoom has a SelectedValue which <span style="color:Blue; is invalid because it does not exist <span style="color:Blue; in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace <span style="color:Blue; for more information about the error and <span style="color:Blue; where it originated <span style="color:Blue; in the code.

Exception Details: System.ArgumentOutOfRangeException: <span style="color:#A31515; ddlgvRoom has a SelectedValue which <span style="color:Blue; is invalid because it does not exist <span style="color:Blue; in the list of items.
Parameter name: value

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified <span style="color:Blue; using the exception stack trace below.

Stack Trace:

[ArgumentOutOfRangeException: <span style="color:#A31515; ddlgvRoom has a SelectedValue which <span style="color:Blue; is invalid because it does not exist <span style="color:Blue; in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +1765625
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +109
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.Control.DataBindChildren() +201
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +201
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
System.Web.UI.Control.DataBind() +15
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +166
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3896
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +26
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

[/code]

That error. I am desperate to figure out how to fix this I am assuming from what I have been reading that they want me to find the dropdownlist in the gridview and populate it through the code behind However I am lost on how to approach that.

Here is my webservice code which populates the dropdownlist but this works during insert mode

<pre> [WebMethod]
public CascadingDropDownNameValue[] GetRooms(string knownCategoryValues, string category)
{
dsRoomsTableAdapters.RoomTableAdapter roomAdapter = new dsRoomsTableAdapters.RoomTableAdapter();
dsRooms.RoomsDataTable rooms = roomAdapter.GetAllRooms();
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

foreach (DataRow dr in rooms)
{
string room = (string)dr["RoomName"];
int roomid = (int)dr["intRoom"];
values.Add(new CascadingDropDownNameValue(room, roomid.ToString()));
}
return values.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetJacks(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int roomid;
if (!kv.ContainsKey("Jack") || !Int32.TryParse(kv["Jack"], out roomid))
{
return null;
}
dsJacksTableAdapters.JacksByRoomIDTableAdapter jackAdapter = new dsJacksTableAdapters.JacksByRoomIDTableAdapter();
dsJacks.JacksByRoomIDDataTable jacks = jackAdapter.GetJacksByRoomid(roomid);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

foreach (DataRow dr in jacks)
{
values.Add(new CascadingDropDownNameValue((string)dr["JackNumber"], dr["intJack"].ToString()));
}
return values.ToArray();
}
}[/code]

<br/>
Gridview Dropdownlist code

<pre> </asp:DropDownList>

<asp:CascadingDropDown ID="ddlgvRoom_CascadingDropDown" runat="server"
Enabled="True"
TargetControlID="ddlgvRoom"
Category="Jack"
ServiceMethod="GetRooms"
ServicePath="cascadingddl.asmx"
LoadingText="[Loading Rooms...]" PromptText="Please Select Room
</asp:CascadingDropDown>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgvRoom" runat="server" Text=<%# Eval("IntRoom") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JackNumber" SortExpression="IntJack
<EditItemTemplate>
<asp:DropDownList ID="ddlgvJack" runat="server" DataTextField="JackNumber"
DataValueField="intJack" Text=<%# Bind("IntJack") %> >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:CascadingDropDown ID="ddlgvJack_CascadingDropDown" runat="server"
Enabled="True"
TargetControlID="ddlgvJack"
Category="Jack"
LoadingText="[Loading Jacks...]"
ServiceMethod="GetJacks"
ServicePath="cascadingddl.asmx"
ParentControlID="ddldvRoom"
PromptValue="Please Select A Jack
</asp:CascadingDropDown>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgvJack" runat="server" Text=<%# Bind("IntJack") %>></asp:Label>
</ItemTemplate>[/code]

If anyone can help please I am desperate tried couple other forums and nobody seems to want to help must be more than a headache than its worth but I am at a dead end and really want to figure this out. If you need more of my code I will be happy to post
it i have MSSQL Stored procedures but they all run fine as this does work Just not in editmode.

Thanks

View the full article
 
Back
Top