Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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

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

Avatar of it_saige
it_saige
Flag of United States of America image

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-
Also, using this method is not specific to type [Windows Application vs. Web Application]

-saige-
Avatar of Mike Eghtebas

ASKER

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"
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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