0

I write class when the program run, it check Connection to SQL Server. if the connection is not Ok, the new form Show up and ask for ServerName, DBName, UserName, and Password , then check the connection again if it is Ok it enter the program

I write Code if new information For connection Ok It save in App.Config But It doesn't Work

public static void SaveToConfig(string key,string value)
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.Settings[key].Value = value;
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

    }
Mishaa
  • 97
  • 1
  • 13
  • if you run it from visual studio, you should check the file ending with vshosts.exe.config – Ofir Winegarten Aug 18 '18 at 10:14
  • did you check this one? https://stackoverflow.com/questions/925638/add-values-to-app-config-and-retrieve-them – TAHA SULTAN TEMURI Aug 18 '18 at 10:25
  • @taha-sultan-temuri Yes I checked Them. and Not Helping – Mishaa Aug 18 '18 at 10:39
  • @Mishaa can you please show me the error? – TAHA SULTAN TEMURI Aug 18 '18 at 10:39
  • Why don't you just store the information in a static class or in user settings? Using configuration as a read/write data store is a misuse. – Crowcoder Aug 18 '18 at 11:01
  • @taha-sultan-temuri , there is no error, the problem is it can't write or update . when i enter the correct connection information, it enter the my program and every thing is fine but when i exit the program and again enter, i expect the connection has been saved but it is not and need all information again – Mishaa Aug 18 '18 at 11:06
  • this is because you are doing temporary updates,so anything inside the appconfig reinitialize when you restart your program,the solution is to save the settings inside registry using serialization or somewhere else to read from there when app starts.you cannot alter the appconfig file during program run because it is being used by your program. – TAHA SULTAN TEMURI Aug 18 '18 at 11:10
  • OpenExeConfiguration() has sharp edges that can easily cause blood loss. If your program is properly deployed to c:\program files then you can only Save() reliably when your program runs elevated. In general the correct way to do this is to run *another* program that updates the config file. It can ask for administrator rights [like this](https://stackoverflow.com/a/2818776/17034). – Hans Passant Aug 18 '18 at 12:16
  • You should write the modified value back to the registry instead of trying to update the app.config. Then your configuration reader should have a fallback mechanism - check the registry first, then if the value doesn't exist try and fetch it from the app.config. – slugster Aug 18 '18 at 12:21

1 Answers1

-1

Try this,

First, include using a statement like :

1 using System.Configuration;

2 Edit App.config so that you add your settings in the appSetting.config file and add an element with key and value parameters e.g.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

    <appSettings>
      <add key="server" value="localhost" />
      <add key="port" value="5432" />
      <add key="username" value="myusername" />
      <add key="password" value="mypass" />
      <add key="database" value="mydb" />
    </appSettings>
</configuration>

Now you can access your settings in your given code.