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

Bound controls error when inserting

3 Answers 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 21 Nov 2013, 04:35 AM
I have a RadGrid...
 
<telerik:RadGrid ID="RadGrid_1" runat="server" DataSourceID="EntityDataSource_1"
CellSpacing="0" GridLines="None"
AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True"
AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
AllowAutomaticUpdates="True" Width="970px">

Im using the EditFormTemplate to show a table with various controls. some are simple text boxes others (dropdownlists) are bound to a different datasource to get predefined values form the DB. 

Everything is working well, I can display a list of records, page, sort, filter, and edit a single record without a problem. 
however, when I try to insert a new record i get a YSOD sayiing the selected value is not in the datasource 
. in the case of the ddl the initial selected value would be "" and since that value does not exist in the datasource I can see why the error is thrown. I have mitigated this by adding in a bank listitem and appending databound items but this seems like a workaround and not a solution.

In the case of a telerik:RadDateInput...
<telerik:RadDateInput ID="RadDateInput44" runat="server" SelectedDate='<%# Bind("effectivedate")%>'   />

 im setting the selected date to a value from the radgrids datasource, again all of the features work until i try to insert a new record.  Because the control is bound the selecteddate will be "" which cant be converted to a real date so i get a {"Specified cast is not valid."} exception.

How can this situation avoided on the insert and still have data bound controls on the edit/update
i was thinking of some kind of conditional binding expression but im not familiar with how to do that

Thanks, Scott


3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Nov 2013, 05:48 AM
Hi Scott,

In case of the DropDownList, set the DataTextField and DataValueField of the control. Instead of RadDateInput, try using RadDatePicker, and set its DbSelectedDate. Below is a sample code snippet that I tried, please have a look.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="EntityDataSource1". . . >
    <MasterTableView CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="OrderDate" HeaderText="OrderDate" UniqueName="OrderDate">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
            </telerik:GridBoundColumn>
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                    style="border-collapse: collapse;">
                    <tr>
                        <td>
                            <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                <tr>
                                    <td>
                                        OrderDate:
                                    </td>
                                    <td>
                                        <telerik:RadDatePicker ID="OrderDateDatePicker" runat="server" DbSelectedDate='<%# Bind("OrderDate") %>'>
                                        </telerik:RadDatePicker>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        ShipCity:
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlShipCity" runat="server" SelectedValue='<%# Bind("ShipCity") %>'
                                            DataSourceID="EntityDataSource1" AppendDataBoundItems="True" DataTextField="ShipCity"
                                            DataValueField="ShipCity">
                                            <asp:ListItem Selected="True" Text="Select" Value="">
                                            </asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="2">
                            <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                            </asp:Button
                            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                CommandName="Cancel"></asp:Button>
                        </td>
                    </tr>
                </table>
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

Thanks,
Princy




0
Scott
Top achievements
Rank 1
answered on 21 Nov 2013, 07:41 PM
Princy, Thank you!

Switching to DataPicker solved the issue and setting the DDL to have a blank item also worked.

How can I overcome a similar issue with this radButton. 

<telerik:RadButton ID="RadButton49" runat="server" ButtonType="ToggleButton" ToggleType="CheckBox" AutoPostBack="false" Checked='<%# Bind("inactive")%>'>
    <ToggleStates>
        <telerik:RadButtonToggleState Value="True" />
        <telerik:RadButtonToggleState Value="False" />
    </ToggleStates>
</telerik:RadButton>

essentially the same condition occurs. Since "Checked" is bound to either "True" or "False" a New record throws an exception because the initial vaule is Nothing. This is the exception that is thrown {Conversion from type 'DBNull' to type 'Boolean' is not valid.}

Normally, I would handle this in the code behind but this page is all declative and there is no code behind.
0
Princy
Top achievements
Rank 2
answered on 22 Nov 2013, 09:25 AM
Hi Scott,

The problem lies in the checkbox control not being able to convert a null value returned from the data source to a boolean value. Please try the following code snippet.

ASPX:
<telerik:RadButton ID="RadButton49" runat="server" ButtonType="ToggleButton" ToggleType="CheckBox"
AutoPostBack="false" Checked='<%# (DataBinder.Eval(Container.DataItem,"inactive") is DBNull ?false:Eval("inactive")) %>'>
        <ToggleStates>
            <telerik:RadButtonToggleState Value="True" />
            <telerik:RadButtonToggleState Value="False" />
        </ToggleStates>
</telerik:RadButton>

Thanks,
Princy
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Scott
Top achievements
Rank 1
Share this question
or