. Advertisement .
..3..
. Advertisement .
..4..
I am working on csharp, but I found the following warning message:
The ConnectionString property has not been initialized
Is there any way to stabilize the issue “the connectionstring property has not been initialized”? I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me? Please find the beginning command below:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=localhost\sqlexpress;Initial Catalog=mydatabase;User Id=myuser;Password=mypassword;" />
</connectionStrings>
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
MySQLHelper.ExecuteNonQuery(
ConfigurationManager.AppSettings["ConnectionString"],
CommandType.Text,
sqlQuery,
sqlParams);
The cause:
You have got the error ”the connectionstring property has not been initialized” due to
ConfigurationManager. AppSettings["ConnectionString"]
tried to look forConnectionString
in theAppSettings
but it did not find anything. The reason is you did not initialize the feature ofConnectionString
forAppSettings
, so the trouble happened.Solution:
The
ConnectionString
should be initialized and referenced as the following:If you do that, your error will completely disappeare. And your program will run successfully. Wishing you lots of lucks!
This error occurs when a datasource tries to bind to data but fails to find the connection string. This error isn’t usually caused by an error in web.config, though I’m not 100% certain.
If you programmatically assign a datasource (such a SqlDataSource), or create a query (i.e. using a SqlConnection/SqlCommand combination), make sure you assigned it a ConnectionString.
When you hook up a databound element with a datasource (e.g. If you are connecting a GridView to a datasource (i.e., a ComboBox to an SqlDataSource), ensure that the datasource is assigned one of your connection strings.
We can look at your code if you post it (for the web.config and databound elements).
EDIT The problem is that you’re trying to retrieve the Connection String from AppSettings, but programmatically it is not there. Replace that with
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
if ConnectionString refers to the name of your connection string.