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.
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.
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.
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.
Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.
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