0

When trying to open one of my window forms in Visual Studio, I get this message box before the page loads: "Input string was not in a correct format". Once I click ok on that message box my page opens correctly with no errors showing.

On the window form that I'm loading I've got a ComboBox and a CheckedListBox, where I'm getting information from a data table in Sql Server.

Could the problem be the conversions I'm doing in my methods? And if so how can change them so that the message box does not appear any longer. I've read that try parse would be better but I'm not really sure how to apply that here.

void CheckList_Bikes()
{
    int idcl = Convert.ToInt32(client.SelectedValue.ToString());
    com.Parameters.Clear();
    com.Parameters.AddWithValue("@idclient", idcl);
    adaptb.Fill(biciT);
    bikes.Items.Clear();
    bikes.DataSource = biciT;
    bikes.ValueMember = "ID";
    bikes.DisplayMember = "name";
}

private void client_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        int idcl = Convert.ToInt32(client.SelectedValue.ToString());
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@idclient", idcl);
        bikes.Clear();
        adaptb.Fill(biciT);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Daniel
  • 5
  • 5

1 Answers1

1

you could check whether an item is selected:

void CheckList_Bikes()
{
    if(client.SelectedIndex != -1)
    {
        int idcl = Convert.ToInt32(client.SelectedValue.ToString());
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@idclient", idcl);
        adaptb.Fill(biciT);
        bikes.Items.Clear();
        bikes.DataSource = biciT;
        bikes.ValueMember = "ID";
        bikes.DisplayMember = "name";
    }

}

If your format is really wrong and you want to use TryParse to check whether the conversion will work you can do it this way:

void CheckList_Bikes()
{
    int idcl = 0;
    if(Int.TryParse(client.SelectedValue.ToString(), out idcl)
    {
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@idclient", idcl);
        adaptb.Fill(biciT);
        bikes.Items.Clear();
        bikes.DataSource = biciT;
        bikes.ValueMember = "ID";
        bikes.DisplayMember = "name";
    }    
}

EDIT:

If you have spaces in your string you can get rid of them using the String.Trim method:

 if(Int.TryParse(client.SelectedValue.ToString().Trim(), out idcl)
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • The TryParse did work, thank you @Mong Zhu. – Daniel Jan 24 '18 at 10:12
  • @Daniel glad to hear that it works, but how is your format? if it does not allow you to parse the number, then this solution is not much worth or am I wrong?=! :) Do you have may be spaces in the number string? – Mong Zhu Jan 24 '18 at 10:24
  • Yes, it appears I do have some empty space. – Daniel Jan 24 '18 at 10:33
  • @Daniel you can use `Trim` to get rid of the spaces. Check ou my edit. Good fortune – Mong Zhu Jan 24 '18 at 11:44
  • good to know. @Mong Zhu also this might be off topic, but can you cast an eye on this question that I've raised: https://stackoverflow.com/questions/48396881/how-can-i-generate-a-different-image-in-a-visual-studio-picturebox-depending-on – Daniel Jan 24 '18 at 11:47