Solved

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

Posted on 2014-10-02
6
516 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 33

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 33

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 43
Problem to error 4 43
Runtime Exceptions when trying to submit data 28 36
System.Speech 2 11
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

16 Experts available now in Live!

Get 1:1 Help Now