I'm trying to use an App.Config file and class to retireve a database server connection string at runtime in my n-tier app. The code in the App.Config is:
And the code in the class to retireve it is:
When testing the UI layer I get the error: Object reference not set to an instance of an object. Note that
In the class to retrieve the conn string I tried hard coding it in as follows:
And that worked fine. But when I try to return "ConfigurationManager.ConnectionStrings("MyConnName").ConnectionString" I get that error.
It gets really confusing when I tested retrieving the conn string in the data layer using Return ConfigurationManager.ConnectionStrings("MyConnName").ConnectionString
. That worked! But when I try it from the UI, I get that error.
Can someone please shed some light here. Any help or suggestions is greatly appreciated!
Dan
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnName" connectionString="Server=MyServerName;Database=MyDBName;UID=MyUid;PWD=MyPwd;Integrated Security=SSPI" />
</connectionStrings>
</configuration>
And the code in the class to retireve it is:
Code:
Imports System.Configuration
Public Class AppConfig
Public Shared ReadOnly Property ConnectString() As String
Get
Return ConfigurationManager.ConnectionStrings("MyConnName").ConnectionString
End Get
End Property
End Class
When testing the UI layer I get the error: Object reference not set to an instance of an object. Note that
In the class to retrieve the conn string I tried hard coding it in as follows:
Code:
Public Class AppConfig
Public Shared ReadOnly Property ConnectString() As String
Get
Return "Server=MyServerName;Database=MyDB;UID=MyUID;PWD=MyPwd;Integrated Security=SSPI"
End Get
End Property
End Class
And that worked fine. But when I try to return "ConfigurationManager.ConnectionStrings("MyConnName").ConnectionString" I get that error.
It gets really confusing when I tested retrieving the conn string in the data layer using Return ConfigurationManager.ConnectionStrings("MyConnName").ConnectionString
. That worked! But when I try it from the UI, I get that error.
Can someone please shed some light here. Any help or suggestions is greatly appreciated!
Dan