3

I have a simple WinForms app that will eventually be a game. Right now, I'm just working on the data access layer of it and I've run into a snag. I created a separate project called DataAccess and inside of that, I created a local .mdf SQL Server database file. I also created an app.config file to store the connections string.

Here's that config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="TBG_Master"
             connectionString="Data Source=(localdb)\MSSQLLocalDb;Integrated Security=true;AttachDbFileName=|DataDirectory|\TBG_Master.mdf"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

To retrieve the connection string from the app.config file I am using code pulled from this question.

Assembly service = Assembly.GetExecutingAssembly();
ConnectionStringsSection css = ConfigurationManager.OpenExeConfiguration(service.Location).ConnectionStrings;
string cs = css.ConnectionStrings["TBG_Master"].ConnectionString;
return cs;

When it gets to the line where it pulls the connection string using the string as the key, it gives me a null reference exception. After examining the css.ConnectionString collection, the only connection string it has in it looks like this:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

This is clearly not what it is in my app.config file, and I know there is no other app.config file in my DataAccess project. Also after debugging, I know for a fact that it is looking in the right assembly.

Why is it giving me this connection string and not the correct one?

Community
  • 1
  • 1
Kyle Rone
  • 305
  • 1
  • 14
  • 5
    The connection string needs to exist in the project *that executes*, not library projects: e.g. it needs to be in your Winforms project's app.config. –  Feb 21 '17 at 16:56
  • Ohhhhhhhhhhhhhhhhhhh well then, that makes sense. Ill change my code and report back if it works :) – Kyle Rone Feb 21 '17 at 16:58
  • 3
    app.configs in libraries are for reference, or for compile-time tools. But during runtime, only the executing project's app.config counts. –  Feb 21 '17 at 16:59
  • 2
    How get ONLY connecttionstrings from app.config/web.config, NOT machine.config? In the machine.config is a connection string that is called "LocalSqlServer"; the default provider (as expected) is "System.Data.SqlClient", and the connection string is `data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true",` – Kiquenet Sep 28 '18 at 08:03

2 Answers2

2

As @Amy pointed out, the reason this was not working was because the connection string was not stored in the executing assemblies app.config file. After moving it, it now works as it should. Thank you for the quick help :)

Kyle Rone
  • 305
  • 1
  • 14
2

You can also use the machine.config file to hold connection strings that are independent of any application for a given machine. That way they don't have to be in any app.config file, because all applications automatically read the machine.config file on the machine they are executing on.

Murray Hertz
  • 112
  • 1
  • 5