Getting connectionString from app.config

Yakkity

Member
Joined
Oct 7, 2009
Messages
7
Programming Experience
1-3
Hi all,

I got a little problem when trying to retrieve my connectionString from app.config. Here's my code:

APP.CONFIG:
---------------
HTML:
<connectionStrings>
        <add name="TestServerString" connectionString="working string"
            providerName="System.Data.Odbc" />
</connectionStrings>

Connection code:
--------------------
HTML:
'Connection = New PsqlConnection("working string")
'And that does work, BUT I want to get it from app.config like this:
ConfigurationManager.ConnectionStrings("TestServerString").ConnectionString
'ConfigurationManager.ConnectionStrings("TestServerString").ConnectionString.toString
'ConfigurationManager.ConnectionStrings("TestServerString")

I have tried it all, but I keep getting this error:
Object reference not set to an instance of an object.

I have the correct imports and reference in my project, read it a thousand times on forums, but I keep having this error ...

Anyone got an idea?

Thanks in advance,
-Yakkity
 
Last edited:
If it's a web app you could use ConfigurationManager.ConnectionStrings() but if it's a WinForms/Console app then just use My.Settings.ConnStringName
 
I'm trying to get the string in my data access layer, which is a class library project. Under which of your 2 options is this? :p
 
You'll have to pass the connection string into the library somehow, class libraries don't have access to the app's My.Settings variable.
 
Back
Top