0

I am working on .NET Framework 4.0 Class Library project along with ADO.NET. I have connectionString inside the App.config which I am trying to read using ConfigurationManager but getting null reference exception.

Error

enter code here

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
 <connectionStrings>
    <add name ="dbConnection" connectionString="Data Source=xyz;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Reading ConnectionString

using System.Configuration;

var x22 = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;

enter image description here

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • 1
    The settings must be in the app.config of the hosting executable it doesn't help if its only in the app.config of a surounding assembly. You might want to take a look how the config looks that reaches your output folder. presumably the setting you expect isn't in there. – Ralf Jul 23 '22 at 15:55
  • I have winForm application separate than class library project and In class Library I have app.config where I am trying to read connection string. I don't have app.config in winForm application, do you believe is because of that? – K.Z Jul 23 '22 at 15:59
  • 1
    The app.config of the executable will be copied to the output folder and the setting should be in that app.config. But again simply look in your output folder and take a look at the MyLovelyApplication.exe.config. You might want to take look at the "Configuration for libraries" section in the docs https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/read-app-settings – Ralf Jul 23 '22 at 16:01
  • you was so right, I moved my `connectionString` to winForm 'App.Config` and it worked – K.Z Jul 23 '22 at 16:04
  • 1
    @K.Z Keep that in mind for future apps. When a library implements or an app implements a config of some kind and those specific values from that config are required to be there for it to work the parent program's (the one including it as a dependency) app config becomes the reference point for all config file related dependencies. –  Jul 24 '22 at 07:44
  • 1
    There is however the hierarchical structure that exists in dotnet that allows you to "override" that requirement by having it placed higher up in the chain or lower down, but that would be something you would need to review for your implementation and in this case would most likely need to be in the higher tier (possibly site level tier if this is an IIS hosted app). –  Jul 24 '22 at 07:46
  • Thanks @AndrewRamshaw for guiding me ...these details will be very useful to me – K.Z Jul 24 '22 at 07:49

2 Answers2

1

In your solution explorer and under reference node, check if assembly System.Configuration has been added related to you needed Configuration Class and there is not any error regarding to your references.

But, your code is correct and I suggest to you try to avoid this error by checking null situation in return value of that command. Maybe config file is not exist in your default path. check config file to be exist then try to read it's data.

Hadi Mazareei
  • 31
  • 1
  • 7
  • Thanks @Hadi .. I have upload screen shot in question, it seems fine to me.. – K.Z Jul 23 '22 at 15:56
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 05:51
1

in app config

<connectionStrings>
    <add name="[name]" connectionString="[YOUR CONNECTION]" />
</connectionStrings>

call your connectionString

 ConfigurationManager.ConnectionStrings["name"].ConnectionString

Ahmed_Amr
  • 93
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 05:39