Reputation: 111
This is my app.config that I added at the root of my project(type:ClassLibrary) :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="POS_DEV_GIL-001" connectionString="Server=FACOX\SQLEXPRESSPOS;Database=POS_DEV_GIL-001;Trusted_Connection=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I added as reference the System.Configuration.dll
In my code, I do that :
using System.configuration;
//rest of the code
String connec = ConfigurationManager.ConnectionStrings["POS_DEV_GIL-001"].ConnectionString;
And I get the following error :
object reference not set to an instance of an object
Do you have a solution?
Upvotes: 0
Views: 4231
Reputation: 10818
You mentioned the project is a Class Library. Meaning on compile it is not an executable, but a DLL. Class libraries will, by default, look to the application that executes it for its config files. What application are you running that uses the class library? Make sure the application that uses this library has an app.config
or a web.config
with the connection string:
<add name="POS_DEV_GIL-001" connectionString="Server=FACOX\SQLEXPRESSPOS;Database=POS_DEV_GIL-001;Trusted_Connection=true" providerName="System.Data.SqlClient" />
Upvotes: 4