Solved

The name 'ConfigurationSettings' does not exist in the current context

Posted on 2014-10-02
6
508 Views
Last Modified: 2014-10-02
Using the following code to store connection string in cnSting, I get an error: The name 'ConfigurationSettings' does not exist in the current context

Question: How can I correct this?

Do I get this error because "string cnString = ConfigurationManager.AppSettings["ConnectionString"];" is for Web applications? I am working on a windows app.

using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Configuration;
namespace CodeSampleCS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static string cnString = "";
        [STAThread]
        static void Main()
        {
            //Using cnNorthwind.xml file
            //XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            //cnString  = xmlReader.ReadElementString("connection");
            
            //Using App.config
            string cnString = ConfigurationManager.AppSettings["ConnectionString"];

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01(cnString));
            MessageBox.Show("msg 2" + cnString);

        }
    }
}

with App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"
            connectionString="TestToGetStringFrom App.config File"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 4
  • 2
6 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40358007
You are using the wrong identifier -

Should be:

CodeSampleCS.Properties.Settings.NORTHWNDConnectionString

Not:

ConnectionString

Additional information:

http://www.codeproject.com/Tips/416198/How-to-get-Connection-String-from-App-Config-in-Cs

-saige-
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40358022
Also, using this method is not specific to type [Windows Application vs. Web Application]

-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40358047
Hi saige,

I was at this site while ago. Bur couldn't make it work. Per your post, I hope I have it write as:

string cnString = ConfigurationManager.AppSettings["CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"];

If so, I am getting the same error.

Comment revised:

I am using: string cnString = ConfigurationManager.AppSettings["strNorthWind"];

after I change the name attribute to "strNorthwind"
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40358117
Perhaps you forgot the System.Configuration reference.

1.  Right-click on References and choose 'Add Reference'Capture.JPG2.  Locate and select System.Configuration from the list of .NET assembliesCapture.JPG3.  Press OK
3.  Verify that System.configuration is in your list of ReferencesCapture.JPG
App.config -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<connectionStrings>
		<add name="CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"
		    connectionString="TestToGetStringFrom App.config File"
		    providerName="System.Data.SqlClient" />
	</connectionStrings>
</configuration>

Open in new window


Program.cs -
namespace AppConfigExample
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(ConfigurationManager.ConnectionStrings["CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"].ConnectionString);
			Console.ReadLine();
		}
	}
}

Open in new window


Produces the following output -
Capture.JPG
-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40358258
I appreciate all the hard work to make this work.

FYI I had using System.Configuration; in place. I did all the rest but now luck so far. Later I will start a new project to see I can make it work.

At lease for now I can use xml file which works fine.

Mike
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40358268
My apologies, in the heat of copying the Program.cs, I forgot to add the using statements at the top.  

Here is the completed Program.cs -
using System;
using System.Configuration;

namespace AppConfigExample
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(ConfigurationManager.ConnectionStrings["CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"].ConnectionString);
			Console.ReadLine();
		}
	}
}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now