1

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

Community
  • 1
  • 1
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • Are you able to connect database by using this connection string information through ODBC driver. – Bhasyakarulu Kottakota Oct 09 '14 at 01:30
  • I am able to connect to the database via Sql Management Studio, the app just isn't even finding the connection string in the config file is what I am thinking the error is. – Brian Ogden Oct 09 '14 at 01:31
  • Does yours application's app pool pointing to the targeted frame work 4.0. – Bhasyakarulu Kottakota Oct 09 '14 at 01:36
  • I am using IIS Express and so are the other developers that just downloaded the code and it worked for them. The ASP.NET MVC Website is targeting the 4.0 version. – Brian Ogden Oct 09 '14 at 01:38
  • Might be in others machine they have registered 4.0 with IIS and on yours machine registered 4.5 in IIS. Are you able to access any other pages other than which requires database access? – Bhasyakarulu Kottakota Oct 09 '14 at 01:41
  • But I am using IIS Express not IIS, yes I can access pages that do not require DB access – Brian Ogden Oct 09 '14 at 01:46
  • I changed to IIS and same issue. My Application Pool is 4.0, there is no 4.5 App Pool, you just change your Web.config to target 4.5 version, 4.5 is not a Major revision of the .NET framework – Brian Ogden Oct 09 '14 at 01:52

2 Answers2

1

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.

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • 1
    Connections strings should be recovered by name, the name is the identifier for a connection string, not its position in the configuration file (which you have no control over, as you found). – Albireo Oct 15 '14 at 12:34
  • Oh I know, I didn't write the code, its a DLL I have to use though – Brian Ogden Oct 16 '14 at 19:13
0

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>
Louis Michael
  • 398
  • 1
  • 12