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


<h3>Test</h3>
  <SfComboBox TValue="int" TItem="Countries" DataSource="@CountryList">
    <ComboBoxEvents TValue="int" TItem="Countries" CustomValueSpecifier="@customValue"></ComboBoxEvents>
    <ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
</SfComboBox>
@code {
    private IEnumerable<Countries> CountryList;

    private void customValue(CustomValueSpecifierEventArgs<Countries> args)
    {
        args.Item = new Countries() { Code = 22, Name = args.Text };
    }
    public class Countries
    {
        public string Name { get; set; }
        public int Code { get; set; }
    }
}


Error
blazor.server.js:19 [2020-10-19T18:06:10.088Z] Error: System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Int32'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at System.Reflection.PropertyInfo.SetValue(Object obj, Object value)
   at Syncfusion.Blazor.DropDowns.SfDropDownBase`1.SetItemValue(String itemValue)
   at Syncfusion.Blazor.DropDowns.SfComboBox`2.UpdateValue()
   at Syncfusion.Blazor.DropDowns.SfDropDownList`2.InitValue()
   at Syncfusion.Blazor.DropDowns.SfDropDownList`2.OnAfterRenderAsync(Boolean firstRender)
   at Syncfusion.Blazor.DropDowns.SfComboBox`2.OnAfterRenderAsync(Boolean firstRender)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)


7 Replies 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team October 20, 2020 10:27 AM UTC

Hi Dave, 

Thanks for contacting Syncfusion support. 

We had checked your query. The cause of the reported issue is that you have provided the TValue as int. In C #, the default value of int is 0. Then it will check for the corresponding value of the '0' in the data source. We suggest you to use the TValue as int? to get rid of this issue. 

<h3>Test</h3> 
<SfComboBox TValue="int?" TItem="Countries" DataSource="@Country"> 
    <ComboBoxFieldSettings Text="Name" Value="Code "></ComboBoxFieldSettings> 
    <ComboBoxEvents TValue="int?" TItem="Countries" CustomValueSpecifier="@customValue"></ComboBoxEvents> 
</SfComboBox> 
@code{ 
    private void customValue(CustomValueSpecifierEventArgs<Countries> args) 
    { 
        args.Item = new Countries() { Code = 22, Name = args.Text }; 
    } 
    public class Countries 
    { 
        public string Name { get; set; } 
        public int Code { get; set; } 
    } 
    private List<Countries> Country = new List<Countries> 
    { 
    new Countries() { Name = "Australia", Code = 1 }, 
    new Countries() { Name = "Bermuda", Code = 2 }, 
    new Countries() { Name = "Canada", Code = 3 }, 
    new Countries() { Name = "Cameroon", Code= 4 }, 
    }; 
} 


Regards, 
Ponmani M 



DN Dave Nixon October 21, 2020 12:59 PM UTC


 <p>ComboBox Text is:<strong>@text</strong></p>

<SfComboBox @ref="comboboxObj" TValue="int?" TItem="Countries" ShowClearButton="false" Placeholder="e.g. Australia" Autofill="true" @bind-Value="@ComboVal" DataSource="@Country">
    <ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
    <ComboBoxEvents TValue="int?" TItem="Countries" ValueChange="onChange"></ComboBoxEvents>
</SfComboBox>

@code {
    SfComboBox<int?, Countries> comboboxObj;

    public int? ComboVal = 1;

    public string text;

    public class Countries
    {
        public string Name { get; set; }

        public int? Code { get; set; }
    }

    List<Countries> Country = new List<Countries>
{
    new Countries() { Name = "Australia", Code = 1 },
    new Countries() { Name = "Bermuda", Code = 2 },
    new Countries() { Name = "Canada", Code = 3 },
    new Countries() { Name = "Cameroon", Code= 4 },
    };
    public void onChange(ChangeEventArgs<int?, Countries> e)
    {
        text = this.comboboxObj.Text;
    }
}

Adding Autofill="true" causes error


PO Prince Oliver Syncfusion Team October 22, 2020 06:03 AM UTC

Hi Dave,  

We can replicate the reported issue in our end with the provided information. We have consider this as a defect in our end, the fix for the issue will be included in the upcoming patch release scheduled on October 28, 2020. You can track the status of the issue in the following feedback link  


Regards,  
Prince 



DN Dave Nixon replied to Prince Oliver December 1, 2020 11:04 PM UTC

Hi Dave,  

We can replicate the reported issue in our end with the provided information. We have consider this as a defect in our end, the fix for the issue will be included in the upcoming patch release scheduled on October 28, 2020. You can track the status of the issue in the following feedback link  


Regards,  
Prince 


Tried again with 18.3.0.52

System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Int32]'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at System.Reflection.PropertyInfo.SetValue(Object obj, Object value)
   at Syncfusion.Blazor.DropDowns.SfDropDownBase`1.SetItemValue(String itemValue)
   at Syncfusion.Blazor.DropDowns.SfComboBox`2.CustomValue()
   at Syncfusion.Blazor.DropDowns.SfComboBox`2.FocusOutHandler(FocusEventArgs args)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Syncfusion.Blazor.Internal.SfBaseUtils.InvokeEvent[T](Object eventFn, T eventArgs)
   at Syncfusion.Blazor.Inputs.Internal.SfInputBase.onBlur(FocusEventArgs args)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

It works fine if you type a name that exists in list however if you type something wrong and select off of combo it throws error.
The same thing happens if you set the bound item to a value not in combobox.




Attachment: ComboBox_82e0b631.zip


PM Ponmani Murugaiyan Syncfusion Team December 2, 2020 12:10 PM UTC

Hi Dave, 

Thanks for the update. 

We have checked the reported issue. We would like to inform you that, when enabled the AllowCustom property, the Value field and Value must be string type. Otherwise, you need to send the Item in CustomValueSpecifier event as like below code to get rid of the reported issue. 

Please find the release notes for your reference. 


<SfComboBox @ref="comboboxObj" TValue="int?" TItem="Countries" ShowClearButton="false" Placeholder="e.g. Australia" Autofill="true" @bind-Value="@ComboVal" DataSource="@Country"> 
    <ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings> 
    <ComboBoxEvents TValue="int?" TItem="Countries" ValueChange="onChange" CustomValueSpecifier="@customValue"></ComboBoxEvents> 
</SfComboBox> 
 
@code { 
    ... 
    private void customValue(CustomValueSpecifierEventArgs<Countries> args) 
    { 
        args.Item = new Countries() { Code = 22, Name = args.Text }; 
    } 
    ... 
} 


Kindly check with the above suggestion to get rid of the reported issue. Please get back us if you need further assistance. 

Regards, 
Ponmani M


DN Dave Nixon December 4, 2020 05:28 PM UTC

I am not using AllowCustom.
All I am doing is using ComboBox as shown.  Everything works fine if you select an item in list.  But if you start typing something wrong and select off of box it faults.  If you enter a number that isn't in the list it faults.



PM Ponmani Murugaiyan Syncfusion Team December 7, 2020 01:30 PM UTC

Hi Dave,  

Thanks for the update.  

Solution1 : The AllowCustom API is true by default in ComboBox component. This API specifies whether the component allows user defined custom values (Values which does not exist in data source). So if you don’t want allow data which is not in DataSource, to be added in the control. Then you can disable the AllowCustom property. This will prevent the reported console error. 


<SfComboBox @ref="comboboxObj" TValue="int?" AllowCustom="false" TItem="Countries" ShowClearButton="false" Placeholder="e.g. Australia" Autofill="true" @bind-Value="@ComboVal" DataSource="@Country"> 
    <ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings> 
    <ComboBoxEvents TValue="int?" TItem="Countries" ValueChange="onChange"></ComboBoxEvents> 
</SfComboBox> 

 
Solution2: If you want to allow custom data (user defined value which does not exist in data source) to be added in ComboBox component, then we suggest you use the solution provided in our previous update. So whenever the TValue is int, with the combination of AllowCustom property is true, you need to send the Item in CustomValueSpecifier event.     

We request you to check with the above suggestion. Please get back us if you need further assistance.  

Regards,  
Ponmani M 


Marked as answer
Loader.
Up arrow icon