2

I have a class library in C# has has several classes and a app.config. I have put ConnectionString in app.config as follows:

<connectionStrings>
    <add name="TRN_DB"
         connectionString="Database=TRN;Server=NDTSSN;User ID=ln_users2;Password=%4nu$r!;Connection Timeout=30;pooling=false;" 
         providerName="System.Data.SqlClient"
    />
</connectionStrings>

I try to open connection string in one of the classes in my project as follows:

     string connectionString = ConfigurationManager.ConnectionStrings["TRN_DB"].ConnectionString.ToString();
       SqlConnection con = new SqlConnection(connectionString);
       con.Open();

But it gave me an error as:

{"Object reference not set to an instance of an object."}

The connection string is correct when I use same way in web project inside the web.config, Does anybody know how how I can access it in app.config? I already use it this way too:

 string connectionString = System.Configuration.ConfigurationSettings.AppSettings["TRN_DB"];

this one give me this error:

This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.Appsettings

To make it clear, I don't have any other project in my solution, as I want my class library to work independently. I am not able to app web project and web.config to that. So I either should add connection string to app.config and access it Or add a web.config to my class library project. That I am not sure which one is doable.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alma
  • 3,780
  • 11
  • 42
  • 78
  • On what line are you getting the NullReferenceException? – Jason P Jun 26 '13 at 00:43
  • Wait, if this is just a class library, how are you running the program to get that error? If you've referenced the library from another application, you need to put the connection string in that application's config file. – Jason P Jun 26 '13 at 00:46
  • I am testing it with simple windows form , but I should hand only class Library to the another programmer so where I have to put connection string? – Alma Jun 26 '13 at 00:48
  • I guess I can out it in a separate class in my class library project no? – Alma Jun 26 '13 at 00:49
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 26 '13 at 01:49

6 Answers6

5

If you are running Web project with Class library, add connection string to web.config inside the Web project

If you are running Console/WinForm project with Class library, add connection string to app.config inside the Console/WinForm project

modifying config in Library project will not work in your case.

Neverever
  • 15,890
  • 3
  • 32
  • 50
3

From what I understand, you have a class library with a configuration file with a connection string and are referencing that library in another app. The problem is an app.config (or web.config) is relative to the executing application.

Say your class library is named Shared.dll and have a Shared.dll.config and another application (let's say it's a Console Application) called Application with Application.exe.config. When running that application, the app.config used will be Application.exe.config.

If you are trying trying to access a connection string named TRN_DB in this application, it will look in the application's configuration file where it is not defined and return null (hence the Reference Exception).

Simon Belanger
  • 14,752
  • 3
  • 41
  • 35
  • Oh ok Then I guess it is not possible, I should create a web project and put connection string in the web.cofig of that. – Alma Jun 26 '13 at 00:52
1

In windows forms applications always add reference to System.Configuration in References then in the code page add using System.Configuration then use the connection strings like this:

string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
1

I had a similar problem and the issue turned out to be that I needed to add System.Configuration to the References.

I thought adding using System.Configuration; to the class would do the trick but I had to add System.Configuration to the references before it would work.

Deathstalker
  • 794
  • 10
  • 8
0

The connection string stuff needs to be in the main exe project's app.config or else you need to load it manually.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0

As others have said, the library will use the config file for the executing application.

I'd like to add one suggestion though. One option to consider, for (in my opinion) a bit of polish and a professional touch is creating a custom configuration section. That would allow you to group the settings for your library and not leave any confusion about which settings are used by your library: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

Jason P
  • 26,984
  • 3
  • 31
  • 45