2

I am writting a windows service, and I catch an exception using try:

try
{
    connStr = System.Configuration.ConfigurationManager.AppSettings["connStr"].ToString();
}
catch (Exception ex)
{

    logger.Error("get the connection string failed,detail:" + ex.ToString());
}

The output is:

get the connection string failed,detail:System.NullReferenceException: not set an instance with a object reference.

It can't get the connection string correctly.

And this is my configuration file(app.config) :

<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
        <add key="connStr" value="Data Source=Dolphin-PC;Initial Catalog=jsptpd_SYS;Persist Security Info=True;User ID=sa;Password=ccir"/>
    </appSettings>
</configuration>

Where is wrong? Why can't get the connection string?

I've been searching from google and can't find where is wrong?

Some reason can cause the problem?

The stack track:

 2013-12-13 21:37:19,895 [17] ERROR ApplicationInfoLog [(null)] <(null)>
   - get connection string failed,detail:
     System.NullReferenceException:  not set an instance with a object reference.
     on Jsptpd.JobScheduler.jsptpdJobScheduler.OnStart(String[] args) location     

 D:\jsptpd\Code\jsptpdJobScheduler\jsptpdJobScheduler\jsptpdJobShedule.cs:line 41
7
  • What is your meaning? Commented Dec 13, 2013 at 13:45
  • The stack trace of the exception, it shows where the exception happens. If .NET framework symbols are there, you'll get a more useful stack trace. Commented Dec 13, 2013 at 13:48
  • 1
    You should probably be using the ConnectionStrings property of your ConfigurationManager instead. And, of course, use the configuration file correctly. Commented Dec 13, 2013 at 13:48
  • Go into executable path and check if you have there YourProgrammName.exe.config with proper content, to make sure your config is in place and valid. Commented Dec 13, 2013 at 13:50
  • Not have this file,what should i do? Commented Dec 13, 2013 at 13:57

3 Answers 3

2

Try this one:

connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
Sign up to request clarification or add additional context in comments.

1 Comment

I thought that originally but notice he's actually got the string stored in the AppSettings section of the config file
2

It is because your program location don't have a programName.exe.config file,the ConfigurationManager can't access the content,so make sure the file exist.

Or you can link there to know more about ConfigurationManager :

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

Comments

1

You're looking in the wrong part of the ConfigurationManager.

Try putting the ConnectionString in the ConnectionStrings area of the web.config and calling

connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"];

If you're still having issues put a breakpoint on the line and see which ConnectionStrings are being loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.