views:

1521

answers:

2

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
A: 

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

*)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 :

  • 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

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.

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.

To localize the problem, try to authenticate using SQL Authentication, if it's works, that means this Integrated Security is the culprit

Can you check the user account who run the application pool of the current ASP.NET web application ?

hadi teo
Thanks for the ideas.I receive the error when my application starts up because one of the first things it does is to connect to the database.The connection string works. I modified it on my PC to connect to the production db just to be sure and it connected.I've also tested being able to connect to the production db various ways (through VS Server Explorer, using DSN {on my pc, dev server, prod server}, and setting up a connection through SQL Server {on my pc, dev server, prod server}).All servers are in the same network. So no firewall issues.Any other thoughts?
@Jason, Please refer to my updated post.
hadi teo
I am using a SQL Server user for my connectionstring with SQL Server and Window Authentication set. I didn't really find anything helpful regarding PermissionDemand. I did figure out the problem though. See my answer below.
A: 

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!

Thanks Hadi for your time and help.

@Jason, you're most welcome. Thanks for sharing your information in how to resolve this issue. Apparently i overlook the IIS settings.
hadi teo