Solved

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

Posted on 2014-10-02
6
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 34

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 34

Expert Comment

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

-saige-
0
 
LVL 34

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 34

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 34

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 34

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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