This is a migrated thread and some comments may be shown as answers.

Problem because same edit form template used for updates and inserts

6 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 29 Jul 2009, 03:18 PM
Hi

I am using an edit form template within a radgrid. My edit form template is used to update Customer records. One of the fields in my customer table is TitleID which is a foreign key to the Titles table. This table contains records such as Mr, Mrs Miss etc. The form template contains a drop down list of these titles which is populated from the database. The SelectedValue property of the dropdown list is set to the TitleID of the current record. This is achieved like so

 <asp:DropDownList ID="cboTitle" runat="server" DataTextField="Title" DataValueField="TitleID" 
SelectedValue='<%# Bind("ContactTitleID") %>'     DataSource='<%# (GetTitles()) %>'>  
                </asp:DropDownList><br /> 

This code works fine for automatic updates. However when i try to perform an insert I get an error binding to the drop down list

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

I understand what is causing this error. Naturally for an empty record the value for Bind("ContactTitleID") is an empty string whereas this is not one of the items in the drop down list of titles.

How can i overcome this whilst still performing automatic inserts and updates? I can populate the drop down list of titles and the selected value in the ItemDataBound event but then the SelectedValue property of the of the cboTitles dropdown list is not passed to the ContactTitleID field because this control is not bound to this field.

I think this problem is avoided in standard grids because they have different templates for inserts and updates.

Many thanks

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 30 Jul 2009, 04:29 AM
Hello Andrea,

Refer to the following help document which explains on how to avoid similar errors:
'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items

Hope this helps...
Princy.
0
bob
Top achievements
Rank 1
answered on 08 Nov 2011, 09:50 PM
The link is out of date now, can you post a valid link please.
0
Shinu
Top achievements
Rank 1
answered on 09 Nov 2011, 08:42 AM
Hello Bob,

Check the following help documentation.
Known Reasons for Error Messages.

-Shinu.
0
Iana Tsolova
Telerik team
answered on 09 Nov 2011, 10:05 AM
Hello Bob,

Here is the proper link:
http://www.telerik.com/help/aspnet-ajax/grid-error-messages.html

Kind regards,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
bob
Top achievements
Rank 1
answered on 09 Nov 2011, 04:05 PM
Thanks.
0
Jhess
Top achievements
Rank 1
answered on 25 Jan 2012, 12:45 PM
Hi Pincy,

I did all what steps on the link but i still get the error "

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

here in my code
asp:
     <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("ohds_statuscode") %>' TabIndex ="7"   
                                        DataSource = '<%# (new string[] {"On-Going","Revert-Back","Closed"}) %>' AppendDataBoundItems="True"
                                        Style="z-index: 1;">
                                        <asp:ListItem Selected="True" Text="New" Value=""></asp:ListItem>
     </asp:DropDownList>

code behind:
 protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //Add new" button clicked
                e.Canceled = true;
                //Prepare an IDictionary with the predefined values
                System.Collections.Specialized.ListDictionary newValues = new
                System.Collections.Specialized.ListDictionary();
                newValues["ohds_statuscode"] = "";
                //Insert the item and rebind
                e.Item.OwnerTableView.InsertItem(newValues);
            }
        }

Thank you so much,
Jhess

Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
bob
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Iana Tsolova
Telerik team
Jhess
Top achievements
Rank 1
Share this question
or