Winforms C # Error with ComboBox nested with mscorlib.dll in Visual Studio

1

Help with the error:

  

'System.FormatException' in mscorlib.dll. Additional information: The input string does not have the correct format.

I have an error when I start my frame for the first time since I have ComboBox nested State, Municipality and Location. The last two are updated with events. Municipality is updated when something changes in State, and Location is updated when there is a change in Municipality.

My problem occurs when the ComboBox Status is filled for the first time, since I imagine that the event cmbEstado_SelectedValueChanged of ComboBox is activated from behind and therefore there is an error in Municipality since it does not recognize the property cmbEstado.ValueMember , which should be the state id. But it is only the first time. Since after loading it does not give me the error anymore.

This is the state filling code

try
{
    List<Estado> estados = ctrl.llenarDataSource<Estado>().ToList();
    cmbEstado.DisplayMember = "stNombre";
    cmbEstado.ValueMember = "id";
    cmbEstado.DataSource = estados;
}
catch (Exception ex)
{
    ctrl.msjError(ex.Message + " " + ex.Source);
}

And this one of Municipality

try
{
    cmbMunicipio.DataSource = ctrl.consultarConID<Municipio>("idEstado", int.Parse(cmbEstado.SelectedValue.toString()));
    cmbMunicipio.DisplayMember = "stNombre";
    cmbMunicipio.ValueMember = "id";
}
catch (Exception)
{


}

As you will see I mitigate the error with the empty Catch so that it does not do anything, however, I consider that it is not the best thing to do that. Does anyone have any suggestions?

    
asked by Omar Cerón 04.12.2017 в 02:12
source

1 answer

0

Try validating the following:

  • When the SelectedIndex of the Combo is different from Null
  • When the SelectedIndex of the Combo is > -1
  • When the SelectedItem of the Combo is different from Null
  •   

    Instead of user the Event cmbEstado_SelectedValueChanged test using cmbEstado_SelectionChangeCommitted Event: SelectionChangeCommitted . It occurs when the user changes the selected item and that change is shown in ComboBox.

        
    answered by 23.12.2017 / 15:21
    source