Friday, April 10, 2009

System.InvalidOperationException: The ConnectionString property has not been initialized

Are you seeing the following exception message:

System.InvalidOperationException: The ConnectionString property has not been initialized. at System.Data.SqlClient.SqlConnection.PermissionDemand() at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at xxxxxxx in yyyyyyyy

One cause for this error is, if you try and setup a SqlConnection object with a connection string that is empty (null, nadha, nill, nothing…). So test by hard coding the connection string that you are using to create the SqlConnection. If that works – then the way that you are retrieving your connection string (web.config, custom text/xml file, etc) might be having an issue (permissions, file missing, etc).

1 comment:

Tanner said...

I get this error. Here is my situation.

I have a project which contains a class. This class contains a public property called Connection. I have many sub routines that are wrapped in the "using" block.

What happens to me is that the first routine to execute can open the connection and execute what it wants. The using block then closes the connection. My next routine fires that also is "Using me.connection" and when I reach the part where "If connection.state <> open then open" I throw this exception.

It seems that the using block is not properly closing it, or something else is going on.

If I declare a local variable in the sub and assign a reference to that connection property, then I do not see the exception. This is my workaround for now, but it's a maintenance issue. I want to just be able to share a connection object from the class to anything in my project that may need a door into my database.