How to distinguish selected combobox value to integer?

I have a combo box that was populated using a data source (value and id)

comboBox_Degree.DataSource = ds.Tables["Field"];`
comboBox_Degree.DisplayMember = "Field_Degree";
comboBox_Degree.ValueMember = "Field_ID";

Now I want to restore the identifier when the SelectedIndexChanged event has occurred. but when I passed it to int, I got such an error.

int fid = Convert.ToInt32(comboBox_Degree.SelectedValue.ToString());

Mistake:

The input string was not in the correct format.

How can I apply this value to the whole?

+4
source share
2 answers

Use this:

int fid;
bool parseOK = Int32.TryParse(comboBox_Degree.SelectedValue.ToString(), out fid);
+8
source

I usually do something like

int fid=0;
try {
     fid=int.Parse(comboBox_Degree.SelectedValue.ToString());
    } catch (Exception e)
    {
     //Whatever you want to do when it is not an int
    }
+5
source

All Articles