views:

10386

answers:

3

When I start my application I get: The ConnectionString property has not been initialized.

Web.config:

<connectionStrings>
    <add name="MyDB"
         connectionString="Data Source=localhost\sqlexpress;Initial Catalog=mydatabase;User Id=myuser;Password=mypassword;" />
</connectionStrings>

The stack being:

System.Data.SqlClient.SqlConnection.PermissionDemand() +4876643
System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) +20
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
System.Data.SqlClient.SqlConnection.Open() +122

I'm fairly new to .NET and I don't get this one. I found a lot of answers on Google, but none really fixed my issue.

What does that mean? Is my web.config bad? Is my function bad? Is my SQL configuration not working correctly (I'm using sqlexpress)?

My main problem here is that I'm not sure where to start to debug this... anything would help.

EDIT:

Failling code:

MySQLHelper.ExecuteNonQuery(
ConfigurationManager.AppSettings["ConnectionString"],
CommandType.Text,
sqlQuery,
sqlParams);

sqlQuery is a query like "select * from table". sqlParams is not relevant here.

The other problem here is that my company uses MySQLHelper, and I have no visibility over it (only have a dll for a helper lib). It has been working fine in other projects, so I'm 99% that the error doesn't come from here.

I guess if there's no way of debuging it without seeing the code I'll have to wait to get in touch with the person who created this helper in order to get the code.

+4  A: 

You get this error when a datasource attempts to bind to data but cannot because it cannot find the connection string. In my experience, this is not usually due to an error in the web.config (though I am not 100% sure of this).

If you are programmatically assigning a datasource (such as a SqlDataSource) or creating a query (i.e. using a SqlConnection/SqlCommand combination), make sure you assigned it a ConnectionString.

SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings[nameOfString].ConnectionString;

If you are hooking up a databound element to a datasource (i.e. a GridView or ComboBox to a SqlDataSource), make sure the datasource is assigned to one of your connection strings.

Post your code (for the databound element and the web.config to be safe) and we can take a look at it.

EDIT: I think the problem is that you are trying to get the Connection String from the AppSettings area, and programmatically that is not where it exists. Try replacing that with ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString (if ConnectionString is the name of your connection string.)

Matthew Jones
I added some code, not sure if it makes my problem clearer.
marcgg
I'm not sure if I get it, should I move the location of my connection string in web.config?
marcgg
it's not in the AppSettings btw
marcgg
No, its fine. Just change the code for the MySQLHelper to get the connection string like I did in my edit.
Matthew Jones
changing to ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString doesnt work, but you were close. kscott solution is working, I just need to use the MyDB string
marcgg
thanks for your help, I would have accepted yours as well, but kscott came up with the exact fix before. But youw ere really helpfull, thanks again!
marcgg
+7  A: 

Referencing the connection string should be done as such:

MySQLHelper.ExecuteNonQuery(
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString,
CommandType.Text,
sqlQuery,
sqlParams);

ConfigurationManager.AppSettings["ConnectionString"] would be looking in the AppSettings for something named "ConnectionString", which it would not find. This is why your error message indicated the "ConnectionString" property has not been initialized, because it is looking for an initialized property of AppSettings named "ConnectionString".

ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString instructs to look for the connection string named "MyDB".

Here is someone talking about using web.config connection strings

kscott
It fixed it! Is what I'm doing bad practice?
marcgg
How do you mean? The way you were trying to reference the connection string was incorrect, however if you were trying to reference the value of some node of AppSettings, it would have been correct.
kscott
I meant, is that bad of getting the connection string that way?
marcgg
No, once you are using the ConfigurationManager.ConnectionString["nameOfConnectionString"].ConnectionString method, that is the correct way to get to the connection strings in the web config.
kscott
ok, thanks for your help
marcgg
+1  A: 

The connection string is not in AppSettings.

What you're looking for is in:

System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"]...
GoodEnough