0

I´ve developped a C# program and I have a problem with configuration file. I use Visual Studio 2022 and Windows 10. The DB is SQL Server. To start, the program acesses the configuration file to read some information, then, after the user enters username and password the program grants (or not) use and opens main screen. In config file there are information about server name and DB name. Everything worked fine until some days ago. I made some changes in config file (I removed a variable) and, in my computer it works, with no erros, but in another computer it does't work anymore.An exception appears about connection to DB which is closed. The error message is 'ExecuteReader requires an open and available connection.The state of this connection is closed.' After many prints I foud out that the program is using variables not from the config file, and I don't know where it comes from. I have also another program which deals with this config file to change som variables. This file also can't access config file correctly. Here is a snippet of the code of this second file:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Base.caminho + Base.nomeArquivo + ".exe.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string Banco = config.AppSettings.Settings["Tipo"].Value;
string Servidor = config.AppSettings.Settings["Servidor"].Value;
string Empresa = config.AppSettings.Settings["Empresa"].Value;
string Local = config.AppSettings.Settings["Local"].Value;
string Diretorio = config.AppSettings.Settings["Pasta"].Value;
MessageBox.Show("Arquivo de configuração = " + fileMap.ExeConfigFilename + "\nServidor = " + Servidor + "   Empresa = " + Empresa + "   Local = " + Local +
           "\nDiretório = " + Diretorio + "   Banco = " + Banco);
if (!VerificaBasedeDados(Banco))
{
    MessageBox.Show("Base de dados  " + Banco + "  inexistente!");
    return;
}

In the first MessageBox.Show, the variable Banco (which is the DB name) is different from what is in the config file! The 'if' after tests if thew DB exists, but the program crashes before it. The curious thing here is that in my computer it runs OK. In other computers it doesn't (I tried in 2). And before that change it ran OK too! I tried to include back the variable but it didn't work. Does anyone have any ideas? Thanks.

Ismael
  • 11
  • 1
  • 4
  • 1
    Are the configuration file delivered as a part of the installation or created locally (how?). You write that it works on Your computer - is that from Visual Studio or are You running the exe-file directly? In order for files (config and others) to be copied with the exe-file, you need to set the proper value (*Copy always* or *Copy if newer*) on the file inside Your project. – Claus H Mar 02 '23 at 14:08
  • Hi, Claus. The file is included during ionstallation, but it's necessary to run the configuration program (the second I talked about) to configure the appropriate values for the variables. In may computer the programs work, either by VS ob by clicking on the icons. – Ismael Mar 02 '23 at 15:48
  • Have you confirmed the second computer has the correct configuration file? – topsail Mar 02 '23 at 18:28
  • Yes. I even reinstalled the program, but the problem continues. Any ideas? Thanks. – Ismael Mar 02 '23 at 21:01
  • what are the values at run time of `Base.caminho` and `Base.nomeArquivo` in this line `Base.caminho + Base.nomeArquivo + ".exe.config";` ? This is almost certainly a problem of reading the wrong config file - but it's hard to say anything from just this code alone. Is an `ExeConfigurationFileMap` something you created, or is it a builtin feature? Is `OpenMappedExeConfiguration()` working correctly? Are you providing the right parameters? It is also unclear what you mean by the computer crashing after the first message box is shown. – topsail Mar 02 '23 at 21:18
  • Hi, topsail. As I told in the question, I printed the file name, exactly to check if it was correct. And yes, it was. But together with the file name I printed the variables. One of them, the Db name (Banco) was wrong. Then I changed manually the values in the config file, but the printed results were the same. – Ismael Mar 03 '23 at 13:30
  • As a last try, I erased the config file, for it should give an error, but nothing changed. The printed values continue the same. So, I don't know where the program is reading the values of. But this only happens in other computers. In mine (which has VS) everything works fine. Note that the program stops working stating that there's no open connection. Sure, because the DB name is incorrect. Do you have any suggestions? Thanks. – Ismael Mar 03 '23 at 13:30
  • You just have to find out where the configuration values are being read from. There are many options for loading configuration values. Also I hope you have restarted the application after altering the config because config values may not necessarily be reloaded at runtime. What about my other questions? – topsail Mar 03 '23 at 14:52
  • ExeConfigurationFileMap() is a biltin function in class System.Configuration. The OpenMappedExeConfiguration() function works fine. At least in my computer. Can you think of anything? Thanks. – Ismael Mar 03 '23 at 19:39
  • The whole point of configuration files is that they can be different on different computers. I think you have to forget about the "it works on my machine" idea here. It probably works on their machine too ... they just have a different configuration ;) – topsail Mar 03 '23 at 21:30

0 Answers0