Q:

C# Get ID from combobox item and save it to settings.settings

I set in combobox the Data source from SQL in the Data Binding Mode where I have ID and Name.
Now I want, when the user choose the combobox and choose the name, the ID of the name will be stored in settings.settings. So next time user run the app dont need choose it again.
I try it with:
private void cbWorkingPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            int NameId= Int32.Parse(cbName.SelectedIndex.ToString());

            Properties.Settings.Default.NameId= NameId;
            Properties.Settings.Default.Save();

        }

My problem is, if I change the combobox, then the ID is saved OK, but when I close the app the ID is changed to -1
How can I prevent this??

A:

Do not writing to Settings every time the ComboBox index is changed.
Bind your ComboBox's value to a custom Property, once a certain event is invoked store the data of the Property in Settings. This could be done through a "Save button", which would give the user a clear feedback of what is happening.
An alternative could be to open another view to get to these settings, once there the user needs to close the view to get back, save your settings at that point.
It's always a good idea to validate the data you are saving/using, so binding directly to your data-store isn't such a great idea anyway.

