且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ConnectionString属性尚未初始化错误

更新时间:2022-10-25 22:52:57

这个问题是不是与引黄联接字符串,它是用C#code试图从web.config文件中实现它。

我试图用

System.Configuration.ConfigurationManager.ConnectionStrings[\"techConnectionString\"].ConnectionString;

但我需要使用:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings[\"techConnectionString\"].ConnectionString;

I'm using an asp.net web application with a MySql DB, with the following connetcion string the web.config:

<add name="techConnectionString" connectionString="server=vm-tmysql01;User Id=user;database=tech;password=pass" providerName="MySql.Data.MySqlClient"/>

And this is the code I'm using the get the connection string from the web.config:

System.configuration.configurationmanaget.connectionstrings["techConnectionString"].connectionstring;

For some reason I get the error:

The ConnectionString property has not been initialized. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

And the stack trace is:

[InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +6316916
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6296606
   System.Data.SqlClient.SqlConnection.Open() +300

I'm able to connect to the DB via the Data Sources in Visual Studio and took the ConnectionString from there. I really need some help with it, is there any problem with my connection string?

The problem wasn't with the connetion string, it was with the c# code trying to reach it from the web.config file.

i was trying to use:

System.Configuration.ConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;

But i needed to use:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;