Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm trying to retrieve a SQLServer connection string form my app.config file in my Windows Service project written in C# 4.

I've put at the top...
using System.Configuration;

then I try in my start event...

C#
protected override void OnStart(string[] args)
 {
     try
     {
         cStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();


     }
     catch (Exception e)
     {
         File.AppendAllText(Environment.CurrentDirectory + "\\ServiceMessages.txt", "\r\nOn start Error - " + e.Message);
     }
 }


cStr is an instance string declared earlier.
But it wont compile and my intellisence highlights the ConfigurationManager word saying it doesn't exist in the current context.

What am I doing wrong here?
Thanks.
Posted

You need to use the namespace System.Configuration and also have to add the reference to the assembly System.Configuration.dll

hope it helps :)
 
Share this answer
 
v2
Comments
MarkB123 7-Sep-11 6:43am    
Many thanks - I forgot to add the reference to the assembly under my references - doh!
Uday P.Singh 7-Sep-11 12:08pm    
welcome :)
Have you tried defining the full namespace:
C#
cStr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

or defining
C#
using System.Configuration;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900