I have a local SQL Server database (managed via Management Studio) and a local
ASP.NET project. VS2017 correctly sees the database in the Server Explorer. This is also the place where I got my connection string; as recommended by a few posts on the internet. I placed the string next to the existing one (for the account management) in my web.config
. The file looks like this:
<configuration>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Project-20170524124716.mdf;Initial Catalog=aspnet-Project-20170524124716;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MyLocalDb"
connectionString="Data Source=MYLAPTOP;Initial Catalog=TestDb;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<!--More things-->
</configuration>
I try to access the second string like this:
string connString = ConfigurationManager.ConnectionStrings["MyLocalDb"].ConnectionString;
Which is the way it's done here, here, here and here (among almost every other post on the subject). But, when I run the code I get the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at ProjectUnitTests.DatabaseTests.TestConnection() in ProjectUnitTests\DatabaseTests.cs:line 16
I have the reference to using System.Configuration;
set here. I'm also pretty sure that the formatting on my web.config
is right, given this post made my recheck it about 10 times. I've tried removing the first string, but it had no effect.
The connection string is correct. When I use it directly in my code I can access the database.
Note that the code is run from another project; I have a class library that the code is running from. The Web.config
file is in another project. I've read in my long search for the answer that this could be an issue; although I can't find more sources on this.
Edit:
On request; the entire method from the Unit Tests:
[TestMethod]
public void TestConnection()
{
//Connection tester will be called from the constructor
Database instance = Database.Instance(); //The Database class is in a class library. When calling the instance it will get the connection string itself
string connString = ConfigurationManager.ConnectionStrings["TicketSlothLocalDb"].ConnectionString; //Test if I was able to get the string at all. Apparently this is never possible.
Console.WriteLine(connString);
}
The Database class:
private Database()
{
openConnection();
}
public static Database Instance()
{
_instance = _instance ?? new Database();
return _instance;
}
private void openConnection()
{
try
{
string connString = ConfigurationManager.ConnectionStrings["TicketSlothLocalDb"].ConnectionString;
conn = new SqlConnection(connString);
conn.Open();
openConn = true;
testConnection(); //Function with a small SQL query inside to verify that it is able to retrieve data from the database
}
catch (SqlException ex)
{
Console.WriteLine(ex);
Console.WriteLine("Password is incorrect");
connectionstring = null;
}
catch (Exception ex)
{
Console.WriteLine("Threw other exception: {0}", ex);
}
}
Why can't I access the web.config
property here?