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

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

9 Answers 1849 Views
Grid
This is a migrated thread and some comments may be shown as answers.
miksh
Top achievements
Rank 1
Iron
miksh asked on 01 Oct 2008, 04:58 PM
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?

9 Answers, 1 is accepted

Sort by
0
Missing User
answered on 02 Oct 2008, 12:01 PM
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.
0
miksh
Top achievements
Rank 1
Iron
answered on 02 Oct 2008, 01:26 PM
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)
0
miksh
Top achievements
Rank 1
Iron
answered on 03 Oct 2008, 07:34 PM

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> 

 

0
Missing User
answered on 06 Oct 2008, 10:59 AM
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.
0
miksh
Top achievements
Rank 1
Iron
answered on 06 Oct 2008, 02:53 PM
I opened the ticket.
0
Paul
Top achievements
Rank 1
answered on 14 Feb 2010, 02:08 PM
Hello there

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

Thanks for any help.

Paul
0
Veli
Telerik team
answered on 17 Feb 2010, 03:12 PM
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.
0
Dennis
Top achievements
Rank 2
answered on 14 Oct 2011, 07:29 PM
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
0
Iana Tsolova
Telerik team
answered on 16 Oct 2011, 03:51 PM
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
Tags
Grid
Asked by
miksh
Top achievements
Rank 1
Iron
Answers by
Missing User
miksh
Top achievements
Rank 1
Iron
Paul
Top achievements
Rank 1
Veli
Telerik team
Dennis
Top achievements
Rank 2
Iana Tsolova
Telerik team
Share this question
or