I've looked at a lot of posts on different forums where others have received the same error. Most say they were not referencing the connectionstring from the web.config file correctly, or they were trying to open the connection before setting the connectionstring. Well, if that were the case for me, then how does it work on two different systems, but not on the third? It works on my development PC and on the development server, but not in the production environment. The difference is the web and DB server are separate physical servers in my production environment and on a single server for development. My setup, error message and code will be listed below.
我看了很多不同论坛上的帖子,其他人也收到了同样的错误。大多数人说他们没有正确引用web.config文件中的连接字符串,或者他们在设置连接字符串之前尝试打开连接。好吧,如果对我来说就是这种情况,那么它如何在两个不同的系统上运行,而不是在第三个系统上呢?它适用于我的开发PC和开发服务器,但不适用于生产环境。不同之处在于Web和数据库服务器是我的生产环境和单个服务器上用于开发的独立物理服务器。我的设置,错误消息和代码将在下面列出。
I can simulate the error on my PC if I rename the connection string in either section of the web.config file (appsettings, connectionstrings) to something else. You will see how I have tested both below.
如果我将web.config文件(appsettings,connectionstrings)的任一部分中的连接字符串重命名为其他内容,我可以在我的PC上模拟错误。你会看到我在下面测试过的。
Any help would be GREATLY appreciated for I am in a time crunch to get some idea as to why this won't work in the production environment.
任何帮助都会非常感激,因为我正处于紧张状态,以便了解为什么这在生产环境中不起作用。
Server Error in '/' Application.
The ConnectionString property has not been initialized.
ConnectionString属性尚未初始化。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.
异常详细信息:System.InvalidOperationException:尚未初始化ConnectionString属性。
Line 114: Line 115: Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error Line 116: ErrHandler.LogError(Server.GetLastError) Line 117: End Sub Line 118: Source File: D:\IIS\SSIP\web\App_Code\SitePage.vb Line: 116
[InvalidOperationException: The ConnectionString property has not been initialized.] 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 Database.Open(String connectionstring) in C:\wwwroot\EIIASSIP\common\Core\Database.vb:47 Components.BasePage.get_DB() in C:\wwwroot\EIIASSIP\common\Core\BasePage.vb:41 Components.BasePage.get_ErrHandler() in C:\wwwroot\EIIASSIP\common\Core\BasePage.vb:49 Components.SitePage.Page_Error(Object sender, EventArgs e) in D:\IIS\SSIP\web\App_Code\SitePage.vb:116
'** web.config'
<appSettings>
<add key="ConnectionString" value="Persist Security Info=True;Initial Catalog=[database];Data Source=[server];User ID={0};Password={1};"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=[server];Initial Catalog=[database];Persist Security Info=True;User ID={0};Password={1};" providerName="System.Data.SqlClient"/>
</connectionStrings>
'** BasePage.vb'
Public ReadOnly Property DB() As Database
Get
If m_DB Is Nothing Then
'open database connection
m_DB = New Database
m_DB.Open(ConnectionString) ' ********** Line 41'
End If
Return m_DB
End Get
End Property
Public ReadOnly Property ErrHandler() As ErrorHandler
Get
If m_ErrHandler Is Nothing Then m_ErrHandler = New ErrorHandler(DB)
Return m_ErrHandler '**********Line 49'
End Get
End Property
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
'if using "appSettings" section for connectionstring'
ConnectionString = DBConnectionString.GetConnectionString(ConfigurationManager.AppSettings("ConnectionString"), ConfigurationManager.AppSettings("ConnectionStringUsername"), ConfigurationManager.AppSettings("ConnectionStringPassword"))
'if using "connectionStrings" section for getting connectionstring'
'ConnectionString = DBConnectionString.GetConnectionString(ConnectionStrings.Item("ConnectionString").ConnectionString, AppSettings("ConnectionStringUsername"), AppSettings("ConnectionStringPassword"))'
End Sub
'** ConnectionStrings.vb'
Public Shared Function GetConnectionString(ByVal cs, ByVal Username, ByVal Password) As String
Dim ConnString As DBConnectionString = New DBConnectionString(cs, Username, Password)
Return ConnString.ConnectionString
End Function
'** Database.vb'
Public Sub Open(ByVal connectionstring As String)
RefCount = RefCount + 1
If con Is Nothing Then
con = New SqlConnection(connectionstring)
End If
If Not IsOpen() Then
con.Open() ' ********** Line 47'
End If
End Sub
'** SitePage.vb'
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error
ErrHandler.LogError(Server.GetLastError) '********** Line 116'
End Sub
2 个解决方案
#1
There are several factors included, that you need to test further.
包括几个因素,您需要进一步测试。
*)Please make sure that your connection string is indeed correct. You can try to connect to the same server but different database. But please make sure that the connection string is correct, so that we can eliminate one possibility.
*)请确保您的连接字符串确实正确。您可以尝试连接到同一服务器但不同的数据库。但请确保连接字符串正确,以便我们可以消除一种可能性。
*)Is there any firewall between the DB Server and App Server ? Because the firewall might terminate the existing SQL Server connection if the connection is left open after some time. Some firewall hardware has some configuration to terminate the SQL connection automatically after X time
*)数据库服务器和应用服务器之间是否有防火墙?因为如果连接在一段时间后保持打开状态,防火墙可能会终止现有的SQL Server连接。某些防火墙硬件具有一些配置,可在X时间后自动终止SQL连接
*)I think there is a way or some code snippet to test whether the SQL Connection has been established or not. or Alternatively please do this :
*)我认为有一种方法或一些代码片段可以测试SQL连接是否已经建立。或者请执行此操作:
- Create a file i.e. connection.udl (Make sure that it's not connection.udl.txt , but should be connection.udl) . this file should be created at the APP server.
- Double click that file
- Configure the necessary connection string,
- and finally click Test Connection
- if the test connection works successfully, that means it can connect well to the DB server
创建一个文件,即connection.udl(确保它不是connection.udl.txt,但应该是connection.udl)。该文件应该在APP服务器上创建。
双击该文件
配置必要的连接字符串,
最后单击“测试连接”
如果测试连接成功运行,则意味着它可以很好地连接到数据库服务器
EDIT :
This particular error line is quite interesting : System.Data.SqlClient.SqlConnection.PermissionDemand() as can be seen from the error message stack trace, maybe you need to google around using the PermissionDemand keyword.
这个特殊的错误行非常有趣:从错误消息堆栈跟踪中可以看到System.Data.SqlClient.SqlConnection.PermissionDemand(),也许你需要使用PermissionDemand关键字进行google。
Another thing: have you tried to use Windows Integrated Authentication or SQL Authentication ? Try to use a Windows user OR SQL user and check the result. I see that in your connection string, in the web.config, you are using Integrated Security=true, that means it will take the authentication of the user account who run the application pool of the ASP.NET web application. I suspect this user account doesn't have sufficient permission to access SQL Server.
另一件事:您是否尝试过使用Windows集成身份验证或SQL身份验证?尝试使用Windows用户或SQL用户并检查结果。我在您的连接字符串中看到,在web.config中,您使用的是Integrated Security = true,这意味着它将对运行ASP.NET Web应用程序的应用程序池的用户帐户进行身份验证。我怀疑此用户帐户没有足够的权限来访问SQL Server。
To localize the problem, try to authenticate using SQL Authentication, if it's works, that means this Integrated Security is the culprit
要本地化问题,尝试使用SQL身份验证进行身份验证,如果它可行,则意味着此集成安全性是罪魁祸首
Can you check the user account who run the application pool of the current ASP.NET web application ?
您可以检查运行当前ASP.NET Web应用程序的应用程序池的用户帐户吗?
#2
I figured it out. I decided to look at my applicaiton pool and compare the development server with the production server. I found one difference that fixed the problem.
我想到了。我决定查看我的应用程序池并将开发服务器与生产服务器进行比较。我找到了解决问题的一个区别。
Both servers are using IIS7 since they are on Windows 2008 Server naturally. I went to the Advanced Settings for each application pool and noticed that "Enable 32-bit Applications" under the General section was set to False on the development server where the website works, and True on the production server where it didn't work. So I switched it to False on the production server to get it to work!
两台服务器都使用IIS7,因为它们自然位于Windows 2008 Server上。我转到每个应用程序池的“高级设置”,并注意到“常规”部分下的“启用32位应用程序”在网站工作的开发服务器上设置为False,在生产服务器上设置为True,但它不起作用。所以我在生产服务器上将其切换为False以使其工作!
Thanks Hadi for your time and help.
感谢哈迪的时间和帮助。
#1
There are several factors included, that you need to test further.
包括几个因素,您需要进一步测试。
*)Please make sure that your connection string is indeed correct. You can try to connect to the same server but different database. But please make sure that the connection string is correct, so that we can eliminate one possibility.
*)请确保您的连接字符串确实正确。您可以尝试连接到同一服务器但不同的数据库。但请确保连接字符串正确,以便我们可以消除一种可能性。
*)Is there any firewall between the DB Server and App Server ? Because the firewall might terminate the existing SQL Server connection if the connection is left open after some time. Some firewall hardware has some configuration to terminate the SQL connection automatically after X time
*)数据库服务器和应用服务器之间是否有防火墙?因为如果连接在一段时间后保持打开状态,防火墙可能会终止现有的SQL Server连接。某些防火墙硬件具有一些配置,可在X时间后自动终止SQL连接
*)I think there is a way or some code snippet to test whether the SQL Connection has been established or not. or Alternatively please do this :
*)我认为有一种方法或一些代码片段可以测试SQL连接是否已经建立。或者请执行此操作:
- Create a file i.e. connection.udl (Make sure that it's not connection.udl.txt , but should be connection.udl) . this file should be created at the APP server.
- Double click that file
- Configure the necessary connection string,
- and finally click Test Connection
- if the test connection works successfully, that means it can connect well to the DB server
创建一个文件,即connection.udl(确保它不是connection.udl.txt,但应该是connection.udl)。该文件应该在APP服务器上创建。
双击该文件
配置必要的连接字符串,
最后单击“测试连接”
如果测试连接成功运行,则意味着它可以很好地连接到数据库服务器
EDIT :
This particular error line is quite interesting : System.Data.SqlClient.SqlConnection.PermissionDemand() as can be seen from the error message stack trace, maybe you need to google around using the PermissionDemand keyword.
这个特殊的错误行非常有趣:从错误消息堆栈跟踪中可以看到System.Data.SqlClient.SqlConnection.PermissionDemand(),也许你需要使用PermissionDemand关键字进行google。
Another thing: have you tried to use Windows Integrated Authentication or SQL Authentication ? Try to use a Windows user OR SQL user and check the result. I see that in your connection string, in the web.config, you are using Integrated Security=true, that means it will take the authentication of the user account who run the application pool of the ASP.NET web application. I suspect this user account doesn't have sufficient permission to access SQL Server.
另一件事:您是否尝试过使用Windows集成身份验证或SQL身份验证?尝试使用Windows用户或SQL用户并检查结果。我在您的连接字符串中看到,在web.config中,您使用的是Integrated Security = true,这意味着它将对运行ASP.NET Web应用程序的应用程序池的用户帐户进行身份验证。我怀疑此用户帐户没有足够的权限来访问SQL Server。
To localize the problem, try to authenticate using SQL Authentication, if it's works, that means this Integrated Security is the culprit
要本地化问题,尝试使用SQL身份验证进行身份验证,如果它可行,则意味着此集成安全性是罪魁祸首
Can you check the user account who run the application pool of the current ASP.NET web application ?
您可以检查运行当前ASP.NET Web应用程序的应用程序池的用户帐户吗?
#2
I figured it out. I decided to look at my applicaiton pool and compare the development server with the production server. I found one difference that fixed the problem.
我想到了。我决定查看我的应用程序池并将开发服务器与生产服务器进行比较。我找到了解决问题的一个区别。
Both servers are using IIS7 since they are on Windows 2008 Server naturally. I went to the Advanced Settings for each application pool and noticed that "Enable 32-bit Applications" under the General section was set to False on the development server where the website works, and True on the production server where it didn't work. So I switched it to False on the production server to get it to work!
两台服务器都使用IIS7,因为它们自然位于Windows 2008 Server上。我转到每个应用程序池的“高级设置”,并注意到“常规”部分下的“启用32位应用程序”在网站工作的开发服务器上设置为False,在生产服务器上设置为True,但它不起作用。所以我在生产服务器上将其切换为False以使其工作!
Thanks Hadi for your time and help.
感谢哈迪的时间和帮助。