Click here to Skip to main content
15,910,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public string connectionString = ConfigurationManager.ConnectionStrings["HPDL160G62"].ConnectionString;


throws me
C#
Object reference not set to an instance of an object.
error.

thanks

EDIT MY APP.CONFIG

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="HPDL160G62" providerName="System.Data.SqlClient" connectionString="Data Source=HPDL160G62;Initial Catalog=Database;User ID=username;Password=password;"/>
  </connectionStrings>
  <appSettings>
    
  </appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>


This is in a Class Library and I'm executing from cl app.

What I have tried:

added static keyword and removed but just I don't get why do I get this error with a static class ?!
placed just the top of the class and tried placing into methods but always the same
Posted
Updated 20-Jun-16 1:14am
v4
Comments
CHill60 20-Jun-16 7:08am    
You've mentioned this is a class library being executed from an application - is this the application config or the DLL config ... ConfigurationManager will only read the settings for the currently running assembly ... i.e. the application
Zsolt Madlen 20-Jun-16 7:23am    
This comment is the Real solution for my problem! Thanks
CHill60 20-Jun-16 7:25am    
I'll update my solution in case anyone else has a similar problem
CHill60 20-Jun-16 7:30am    
By the way - you can have an app.config for the DLL - I've included a link in my update that shows how to use one.

1 solution

It will become more obvious what is happening if you break that line down into segments
C#
var x = ConfigurationManager.ConnectionStrings;
var y = x["HPDL160G62"];
var z = y.ConnectionString;

x will not be a problem because, as you say, it's a static class so you don't need an instance.

But y is null because there is nothing in the collection that matches "HPDL160G62".

Therefore you get the error when assigning to z

[EDIT]
The reason that there was no matching item in the collection is because the App.Config belonged to a DLL. ConfigurationManager only reads the app.config for the currently running assembly i.e. the application calling the DLL.

So the solution is to have this connection string in the App.Config for the calling application
OR
You can still have a DLL config but you will have to read it manually (I found a good example of how to do this here[^])
 
Share this answer
 
v2
Comments
Zsolt Madlen 20-Jun-16 6:56am    
yes, you're right. Although I don't know why there is nothing? in app config added the connectionstring as on msdn
CHill60 20-Jun-16 6:58am    
Can you share your App.Config?
Zsolt Madlen 20-Jun-16 7:05am    
added in the main question!
F-ES Sitecore 20-Jun-16 6:59am    
You've either added it in the wrong place, have a typo with the name, or are calling your code from somewhere like a website where config manager is looking at web.config and not app.config.
BillWoodruff 20-Jun-16 7:07am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900