WinForms Application Settings changes don't appear to be persisting
Asked Answered
T

0

0

In my C# WinForm I have a settings window that allows the user to change database connection settings. At design time I set the initial settings that would connect to a database successfully.

I'm accessing those settings in either of two ways:

Either in the Solution Explorer:

enter image description here

Or in the Project's Application Settings:

enter image description here

However, I noticed that if I change any of the values in the Settings.Settings file, the form still loads with the values that I initially set each to.

To be clear: one of the settings is the server, which is an IP address. I initially set it to 256.256.256.256 (dummy IP, of course). The program ran fine and connected to the db fine.

Then I changed the IP address in the Settings.Settings file to 255.255.255.2, which won't connect. But when I run the form it still connected using the originally set IP address.

In my form load event I have this:

db connectionTest = new db();
if (!connectionTest.TestDbConnectionSettings())
{
    DialogResult diagResult = MessageBox.Show("Unable to connect to the database using the entered settings." +
        "\n" +
        "\n" +
        "Click OK to open the Connection Settings window.", "Database Connection Test Failed", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
    if(diagResult == DialogResult.OK)
    {
        frmDbConnectionInfo frm = new frmDbConnectionInfo();
        frm.Show();
    }
}

And here's the TestDbConnectionSettings method in my Db.cs class:

private readonly string connString = string.Format("Server = {0}; Port = {1}; User Id = {2}; Password = {3}; Database = {4};", 
        Properties.Settings.Default.serverSetting,
        Properties.Settings.Default.portSetting,
        Properties.Settings.Default.userIdSetting,
        Properties.Settings.Default.passwordSetting,
        Properties.Settings.Default.databaseSetting);

public bool TestDbConnectionSettings()
{
    try
    {
        Cursor.Current = Cursors.WaitCursor;
        using (NpgsqlConnection conn = new NpgsqlConnection(connString))
        {
            conn.Open();
            Cursor.Current = Cursors.Default;
            return true;
        }
    }
    catch
    {
        Cursor.Current = Cursors.Default;
        return false;
    }

}

So, it should be reading from the settings file (at least that's my intention), but it doesn't appear to be - anymore, at least. On a hunch I placed the code in Db.cs that sets the values for connString into the TestDbConnectionSettings method itself, but that didn't change anything.

Timberland answered 3/4, 2020 at 18:40 Comment(7)
I don't see where you're saving those settings. What does I changed the IP address in the Settings.Settings file mean? Did you edit the file manually or calling Properties.Settings.Default.Save()? + What is the scope of these settings?Pascal
@Jimi, I updated my question with screenshots of where I'm going to those settingsTimberland
I think what @Pascal is getting at is that if the settings were ever saved to disk, then it doesn't matter what your default values were in the Settings.settings file. Your app will continue to read the original IP setting that was saved to disk. So, if you edited Settings.settings (manually, or through the Visual Studio GUI), but you haven't updated the actual settings serialized to disk (at something like C:/Users/<yourusername>/AppData/Local/<assemblycompanyname>/<assemblyproductname>), then the app may still be reading the first value of the setting you chose.Bookkeeper
Yes, but it's not clear what you're doing. If you have saved your settings at least once, then these are the values that are used when you read them. These settings are saved to the AppData Local or Roaming depending on the configuration (in a user.config file). If you manually change a setting in the Project Properties, the saved setting are still the active ones unless you call Properties.Settings.Default.Reset(). This, of course, resets the setting to the default value (which is, usually, considered immutable - unless you mutate it with other means, that is).Pascal
(note about above: the path through <yourusername> is only valid if there is a setting marked with the User scope and not Application scope). And @Pascal says it better.Bookkeeper
@Pascal & Sean, After reading your discussion, I'm not clear if I'm even going about this the right way now. I haven't deployed anything, and haven't done anything directly as far as where those settings are saved. Right now I'm just running this from the VS GUI (F5). I need to allow the user to set connection settings and those would persist for the next time the application was run. They don't need to be user-specific - just whenever the app runs (whoever it is that runs it), those saved settings are used. Is this not the best way to accomplish that?Timberland
When your User sets a new ConnectionString value, you change the value of that Setting then call Properties.Settings.Default.Save(). If you specified a default value (assigning a value to the Setting in the Designer), you can get it back with Properties.Settings.Default.Reset() (note that this will reset all Settings, not just one). The Settings in the User scope are defined per-User (each User can have its own). Default settings are saved in the [Application].exe.config file. The ConnectionString, usually, has its own, specific, setting though.Pascal

© 2022 - 2024 — McMap. All rights reserved.