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

ASP Drop Down Does Not Work for Insert New Record in RadGrid

2 Answers 550 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 12 Jul 2019, 12:09 PM

I have a Telerik RadGrid using an ascx web form user control for editing and adding new records to the grid.

On the web form control I have DropDown setup with this 

<asp:DropDownList ID="ddlComparisonSubjects" runat="server"
                        DataTextField="SubjectName" DataValueField="SubjectId_FK"
                        AppendDataBoundItems="true"
                        SelectedValue='<%# DataBinder.Eval(Container, "DataItem.SubjectId_FK") %>'>
                    </asp:DropDownList>

 

And My C# code is this

 

var SubjectNames = db.T_Subjects.Where(a => a.CaseId_FK == intCaseId).Distinct().ToList();
ddlComparisonSubjects.DataTextField = "SubjectName";
ddlComparisonSubjects.DataValueField = "SubJectId";
ddlComparisonSubjects.DataSource = SubjectNames;
//ddlComparisonSubjects.Items.Clear();
//ddlComparisonSubjects.SelectedIndex = -1;
//ddlComparisonSubjects.SelectedValue = null;
//ddlComparisonSubjects.ClearSelection();
ddlComparisonSubjects.DataBind();
ddlComparisonSubjects.Items.Insert(0, "--Select--");

 

The code lines commented out were added while troubleshooting this issue.  They did not solve the problem so I commented them out.  I have left them so you can see what I have tried to do to fix the problem.

Everything works fine if I am editing an existing record. However when I try to add a new record I get this error message:

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

I know the Parameter names are correct because the drop down works in edit mode.

So why do these drop downs work in edit mode but not in insert mode and how can I fix it?

2 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 16 Jul 2019, 08:42 AM
Hi Perry,

The SubjectId_FK value while creating new record should correspond to some value in the dropdownlist options. Try changing the Value of the item with Text "--Select--" to DataValue.Null or String.Empty.

Alternatively, you can include a ternary if statement in the SelectedValue property or a null coalescing operator, similar to this: 
<asp:DropDownList ... SelectedValue='<%# DataBinder.Eval(Container, "DataItem.SubjectId_FK") ?? "MyDefaultValue"  %>'

I hope this will prove helpful.

Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
PAUL
Top achievements
Rank 1
Iron
commented on 22 Sep 2023, 04:10 PM

I have the same problem and am still looking for an answer but I used telerik:RadDropDownList not asp:DropDownList.

 

Doncho
Telerik team
commented on 27 Sep 2023, 12:37 PM

Hi Paul,

The RadDropDownList normally can be used as a substitution for a DropDownList. For instance, in our Edit Form Types live demo, the DropDownList (ddlTOC), can be replaced with a RadDropDownList with a similar configuration:

<telerik:RadDropDownList ID="ddlTOC" runat="server" SelectedText='<%# DataBinder.Eval(Container, "DataItem.TitleOfCourtesy") %>'
    DataSource='<%# (new string[] { "Dr.", "Mr.", "Mrs.", "Ms." }) %>' TabIndex="7"
    AppendDataBoundItems="True">
    <Items>
        <telerik:DropDownListItem Selected="True" Text="Select" Value="" />
    </Items>
</telerik:RadDropDownList>
<%--<asp:DropDownList ID="ddlTOC1" runat="server" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.TitleOfCourtesy") %>'
    DataSource='<%# (new string[] { "Dr.", "Mr.", "Mrs.", "Ms." }) %>' TabIndex="7"
    AppendDataBoundItems="True">
    <asp:ListItem Selected="True" Text="Select" Value="">
    </asp:ListItem>
</asp:DropDownList>--%>

Could you please share some more details about the problem you are facing? Are there any errors or unexpected behavior? Sharing the markup declaration of the RadGrid along with the relevant code-behind logic would allow me provide you with a more accurate assistance.

0
PAUL
Top achievements
Rank 1
Iron
answered on 27 Sep 2023, 04:48 PM

I was blessed to have found the answer myself to this problem.   I will share it even though it is a bit embarrassing.   If I remember correctly it had to do with the InsertCommand and UpdateCommand within the asp:SqlDataSource.   Abbreviated example of what it was below.

InsertCommand="INSERT   INTO [ThisDB] ( HowId)   VALUES   (@HowId) "
UpdateCommand="UPDATE ThisDB SET  HowId= @RadHowDDL   WHERE  Id = @Id "  

 

The dropDownList looks like below:

<telerik:RadDropDownList ID="RadHowDDL"  runat="server"  AppendDataBoundItems="true"   Width="500px"
DataSourceID="SqlDataSource_HowList"   DataValueField="HowId"  DataTextField="HowValue"               SelectedValue='<%# Bind("HowId") %>' >
 <Items>
<telerik:DropDownListItem Value="0" Text="Select One (If Damaged)" />
</Items>
</telerik:RadDropDownList>

To the best of my present knowledge this is what the problem was, the update would work using the DDL's ID (RadHowDDL) but the Insert would not.  When i did an update the value would be saved for the DDL but when I tried to do an insert the DDL selected value would not be stored in the table, It would always be NULL.   So, I figured there was something else wrong since the update would work using  RadHowDDL  within the UpdateCommand but InsertCommand would not.   I changed the InsertCommand to point to the ID value for the foreign key  (which was HowId),.  This was the same in both tables and the value selected within the DDL was entered into the table instead of NULL.

Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Answers by
Eyup
Telerik team
PAUL
Top achievements
Rank 1
Iron
Share this question
or