Solved

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

Posted on 2014-10-02
6
556 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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