Object of type 'System.Double' cannot be converted to type 'System.Int32'

10 posts, 0 answers
  1. miksh
    miksh avatar
    275 posts
    Member since:
    Nov 2006

    Posted 01 Oct 2008 Link to this post

    I use a RadGrid with ObjectDataSource. For most fields I use autoedit mode but for one field type of Int32 I added  EditItemTemplate with  telerik:RadNumericTextBox and bind it with  DbValue='<%# Bind("myField") %>'.
    The update/insert throws an error "Object of type 'System.Double' cannot be converted to type 'System.Int32'".
    How to fix the issue but still use the Bind() approach?
  2. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 02 Oct 2008 Link to this post

    Hi miksh,


    You can use ObjectDataSource.Updating event to to change the parameter values and types before the ObjectDataSource control performs the update operation. The parameters are available as an IDictionary collection that is accessed by the InputParameters property, which is exposed by the ObjectDataSourceMethodEventArgs object.

    Kind regards,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. miksh
    miksh avatar
    275 posts
    Member since:
    Nov 2006

    Posted 02 Oct 2008 Link to this post

    It happens before the ObjectDataSource.Updating event. It looks like the grid tries to create the object and passes it to ObjectDataSource as a parameter.
    The exception stack is:

       at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
       at System.Web.UI.WebControls.ObjectDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters)
       at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
       at System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback)
  5. miksh
    miksh avatar
    275 posts
    Member since:
    Nov 2006

    Posted 03 Oct 2008 Link to this post

    Maybe it helps to figure out the issue:

    This column successfully updates object's Int32 property  :

    <telerik:GridNumericColumn   
        DataField="OverflowCapacity"   
        HeaderText="Overflow Capacity"   
        UniqueName="Ovr"   
        DataType="System.Int32"   
        NumericType="Number"   
        MaxLength="4">  
    </telerik:GridNumericColumn>                          
     

    This columns fails with "Object of type 'System.Double' cannot be converted to type 'System.Int32'"

    <telerik:GridTemplateColumn   
        HeaderText="Overflow Capacity"   
        UniqueName="OverflowCapacity"   
        DataType="System.Int32">  
        <ItemTemplate> 
            <%# Eval("OverflowCapacity")%></ItemTemplate>  
        <EditItemTemplate> 
            <telerik:RadNumericTextBox   
                ID="txtOvrCap"   
                runat="server"   
                Width="80"   
                MaxLength="4" 
                MinValue="0"   
                MaxValue="9999"   
                Type="Number"   
                ShowSpinButtons="true"   
                DbValue='<%# Bind("OverflowCapacity") %>' 
                NumberFormat-DecimalDigits="0">  
            </telerik:RadNumericTextBox> 
            <asp:RequiredFieldValidator ID="rfvOvr" runat="server" ErrorMessage="Required" ControlToValidate="txtOvrCap">*</asp:RequiredFieldValidator> 
        </EditItemTemplate> 
    </telerik:GridTemplateColumn> 

     

  6. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 06 Oct 2008 Link to this post

    Hi miksh,


    Unfortunately, we weren't able to reproduce the problem that you describe. Could you please open a support ticket and send a sample project that reproduces the case? Thanks in advance.

    All the best,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. miksh
    miksh avatar
    275 posts
    Member since:
    Nov 2006

    Posted 06 Oct 2008 Link to this post

    I opened the ticket.
  8. Paul
    Paul avatar
    42 posts
    Member since:
    Aug 2005

    Posted 14 Feb 2010 Link to this post

    Hello there

    Did you ever get an answer to this problem as I am having the exact same issue?

    Thanks for any help.

    Paul
  9. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 17 Feb 2010 Link to this post

    Hi Paul,

    Try setting the DataType property of the RadNumericTextBox to the data type of the field you are binding the control to:

    DataType="System.Int32"

    Greetings,
    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.
  10. Dennis
    Dennis avatar
    15 posts
    Member since:
    Feb 2009

    Posted 14 Oct 2011 Link to this post

    Veli --

    I have tried your suggestion but I still receive the error.

    An exception occurred. Object of type 'System.Double' cannot be converted to type 'System.Int32'.

    Here is the declarative code for the RadNumericTextBox:

    <telerik:RadNumericTextBox ID="txtTotalConcurrentStudyCapacity" runat="server" 
        Value='<%# Bind("TotalConcurrentStudyCapacity") %>' MaxValue="10000" 
        MinValue="1" ShowSpinButtons="True" Width="85px" NumberFormat-DecimalDigits="0" DataType="System.Int32">
    </telerik:RadNumericTextBox>

    This is a control in the InsertItemTemplate of a FormView that uses an ObjectDataSource.

    However, if I trap the conversion in the _ItemInserting event I can correct the error.

    Private Sub fvForm_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvForm.ItemInserting
        For Each entry As DictionaryEntry In e.Values
            Select Case entry.Key.ToString
                Case "TotalConcurrentStudyCapacity"
                    If e.Values(entry.Key) IsNot Nothing Then
                        e.Values(entry.Key) = CInt(e.Values(entry.Key))
                    End If
            End Select
        Next
    End Sub


    This works but then the question becomes what is the DataType property for on the RadNumericTextBox?

    I am using 2011Q2 RadControls for ASP.Net AJAX.

    Thanks,
    Dennis
  11. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 16 Oct 2011 Link to this post

    Hi Dennis,

    Try using DbValue instead of Value for binding the RadNumericTextBox to a value coming from your databse and see if the issue persists. Also, set the DataType to System.Double if the data field holds Double but Int32.

    Greetings,
    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017