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

Error in FormTemplate

1 Answer 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Timestamp
Top achievements
Rank 1
Timestamp asked on 27 Oct 2010, 11:21 AM
Im using  a FormTemplate like this

<EditFormSettings EditFormType="Template">
         <FormTemplate>
                    Codigo:
                    <asp:TextBox Width="135" ID="CodigoTextBox" runat="server" Text='<%# Bind("Codigo") %>'>
                    </asp:TextBox>
                    Ordem<asp:TextBox ID="OrdemTextBox" runat="server" Text='<%# Bind("Ordem") %>'>
                    </asp:TextBox>

                    <asp:ImageButton ID="objImaInserir" runat="server" ImageUrl="~/Images/InserirEditar.gif"
                         Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%#        (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />

                       <asp:ImageButton ID="objBtnCancel" CausesValidation="False" runat="server" ImageUrl="~/Images/Cancel.gif" Text="Cancel" CommandName="Cancel" />
                </FormTemplate>
 </EditFormSettings>


The field Ordem is a nullable  integer, is not required, so when i dont fill the text box
i get this error.
Failed to set one or more properties on type ERC.DAL.Objecto.   is not a valid value for Int32.

Im using automatic updates and inserts.
If i put the text box in a GridTemplateColumn
like this
  <telerik:GridTemplateColumn DataField="Ordem" HeaderText="Ordem" SortExpression="Ordem"
                    UniqueName="Ordem">
                    <EditItemTemplate>
                        <asp:TextBox ID="OrdemTextBox" runat="server" Text='<%# Bind("Ordem") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="OrdemLabel" runat="server" Text='<%# Eval("Ordem") %>'></asp:Label>
                    </ItemTemplate>
  </telerik:GridTemplateColumn>

  i dont get this error anymore
 
 

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 28 Oct 2010, 07:14 AM
The reason you are getting this error with the FormTemplate is because of the binding expression you are using. When you specify:

Ordem <asp:TextBox ID="OrdemTextBox" runat="server" Text='<%# Bind("Ordem") %>'>

and leave the textbox empty, Bind("Ordem") will take the empty string ("") and attempt to pass it on to the Order field. And an empty string is not a valid Int32 value.

To work around this issue, you can use a RadNumericTextBox, where binding to its DbValue automatically handles null values scenarios:

Ordem:
<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server"
    DbValue='<%# Bind("Ordem") %>' DataType="System.Int32">
    <NumberFormat AllowRounding="true" DecimalDigits="0" />
</telerik:RadNumericTextBox>

To demonstrate this approach, I have created 2 test pages (attached). Default.aspx contains a regular TextBox control databound to the Ordem field in the FormTemplate. When you try to update the item, an exception is thrown indicating the value of empty string cannot be cast to Int32. On the second page (Default2.aspx) there is the above RadNumericTextBox bound to the Ordem field. Null values are handled OK. Check it out.

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Timestamp
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or