Brian Ogden
Brian Ogden

Reputation: 19212

Strange "The ConnectionString property has not been initialized." Error

So have just downloaded the source code for a new project that builds and runs fine on a couple other developer's boxes. I am getting the error:

The ConnectionString property has not been initialized.

I am able to connect to the database via SQL Server Management Studio using the connection string in my Web.config without issue.

The source code for the library that is throwing this error is not available.

The project is an ASP.NET MVC Project with the following values in the Web.config

<connectionStrings>
    <add name="DbConnection" connectionString="Data Source=la-foo-server\development,1444;database=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
    <add key="ConnectionStringName" value="DbConnection" />
</appSettings>

I have tried adding an App.Config to the bin and root of my ASP.NET Website just in case, for some reason, the lib with out source code, is trying to read an App.Config and is ignoring the Web.config.

StackOverflow solutions that have not helped:

A developer had this same situation on a Windows 8.1 environment the other day, he moved to a Windows 7 environment and the error was fixed. I am using Windows 7 pro so I wonder if it is an issue with my .NET Frameworks installations.

The ASP.NET MVC Website is targeting version 4.0. Most of my other projects that all work without issue are targeting .NET 4.5

Upvotes: 1

Views: 4871

Answers (2)

Brian Ogden
Brian Ogden

Reputation: 19212

So I was able to get the source code for the class library that loads the connection string and it turns out the code is loading a collection of connectionstrings like so:

   var collection = System.Web.Configuration.WebConfigurationManager.ConnectionStrings ?? System.Configuration.ConfigurationManager.ConnectionStrings;

        if (collection[1].Name == null)
            throw new Exception("Unable to detect connection string in app.config or web.config! Default connection string name is \'DbConnection.\'");

        db = new Database(collection[1].Name);

My machine.config happened to have a connection string in it for MySql, called LocalMySqlServer. This connection string may have been added by the .NET MySql Connector installation or I may have made the change myself.

So the connectionstring collection was loading a connection from my machine.config, so collection[1] array position did not have the expected connection string in it. This explains why some developers had no issue on their machines and other developers did have issues on their machine.

Upvotes: 1

Louis Michael
Louis Michael

Reputation: 398

Option 1:

<connectionStrings>
    <add name="DbConnection" connectionString="Data Source=la-foo-server\development,1444;Initial Catalog=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Option 2:

  <connectionStrings>
    <add name="DbConnection" connectionString="Data Source=la-foo-server\development;Initial Catalog=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Upvotes: 0

Related Questions