Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConfigurationManager.ConnectionStrings / App.Config problem

Status
Not open for further replies.

Dan777

Programmer
Jul 22, 2000
34
US
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:

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
 
I had a similar issue a while back. I had to set a reference to System.Configuration. After I did that everything worked fine.

Let me know if that does the trick.

Cassidy
 
Thanks for the reply. I already have the System.Configuration reference established. Unfortunately, that's not the answer.
 
Thanks TG for the links. Unfortunately they've just showed me stuff I've already seen. In my app, it works fine when I try to read it and use it from within the same project, which in my app happens to be the data layer, which is compiled as a dll. But when I try to read it and use it in a different project, the business layer, well that's when it doesn't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top