1

I have a console application in which website project is added as dll. Inside dll ConfigurationManager.ConnectionStrings["XXX"] is called which is always returning null.

But web.config in dll has connectionstring named 'XXX'. Can any one suggest what is going wrong?

Dave
  • 7,915
  • 11
  • 62
  • 99
Lydia
  • 145
  • 1
  • 10

5 Answers5

3

I don't think .config file of the .dll is added to your console project together with the dll. You need to have the .config in your executing project.

David S.
  • 5,797
  • 1
  • 39
  • 74
2

Try copying the config file to your console project. Or at least the connection strings section. Probably in an App.config.

Jerry
  • 6,031
  • 7
  • 31
  • 50
1

I had a slightly different problem that I haven't seen an answer to. I was using.

ConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;

When I opened up web.config I saw that the connection string I had created was prefixed with "WebApplicationAPI.Properties.Settings." - putting the entire string in worked for me:

ConfigurationManager.ConnectionStrings["WebApplicationAPI.Properties.Settings.XXXX"].ConnectionString;

0

Your config file is not in the .dll. So you're referencing nothing I would suspect! Unless you've manually copied the web.config file to the correct location or references it via an absolute path?

Dave
  • 7,915
  • 11
  • 62
  • 99
0

web.config is for web application.

For console application you have to use app.config.

Well, actually that's the default configuration for C#, I won't be surprised some advanced user could make it go the other way arround.

Serge
  • 6,156
  • 4
  • 28
  • 54