user3043304
user3043304

Reputation:

c# app not loading user settings

I'm doing a WinForm application, and I've a form which has two TextBoxes, I'm loading values from Settings.Default on form load, like this:

txtUsername.Text = Properties.Settings.Default["email_username"].ToString();
txtAddress.Text = Properties.Settings.Default["physical_address"].ToString();

and I'm saving values back to Settings.Default and then dispose the form when user clicks a button, like this:

Properties.Settings.Default["email_username"] = txtUsername.Text;
Properties.Settings.Default["physical_address"] = txtAddress.Text;
Properties.Settings.Default.Save();

When I close (dispose) the form and then open it again (application still running), I can load settings normally. However, if I close the application then I open the form again (I'm expecting to find data loaded) I find that data is loaded into ONE TextBox but not the other one, which is odd!

Please note that I've tried the following but the same problem still occurs:

Properties.Settings.Default.email_username = txtUsername.Text;

Also, I've tried Application Settings's (Property Binding), but same problem occurs!

NB: when I've debugged the application, I found that it's loading empty "" data into one of the TextBoxes but not into the other one!

So, what's the problem? And How can I solve it?

Thanks!

EDIT: This is a capture of the form, empty fields should have data like other fields!:

https://imgur.com/bSvlk6d

EDIT2:

I'm openning the form like this:

private void mnuEmailSettings_Click(object sender, EventArgs e)
    {
        using (var fms = new frmMailSettings())
        {
            fms.ShowDialog();
        }

    }

The code to load data from settings is inside a procedure LoadData() which resides in from load. I'm closing the form by calling dispose AFTER saving data (I'm using another procedure SaveData().

I've updated the title to not 'loading' after I found that the data exists in user.config in Application's AppData folder.

Upvotes: 0

Views: 1577

Answers (2)

user3043304
user3043304

Reputation:

Sorry to waste your time, but I've found the problem.

At first I thought that Application is not SAVING data, but then after checking user.config I realized that data is saved correctly.

I forgot that I was doing something with the settings in Program.cs. Unfortunately the code was buggy and now everything is working!

Thank you!

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

If you use Settings.Default already, try to access the settings by name instead of accessing the setting by string key.

txtUsername.Text = Properties.Settings.Default.email_username;
txtAddress.Text = Properties.Settings.Default.physical_address;

Upvotes: 1

Related Questions