Mike Eghtebas
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.AppSe ttings["Co nnectionSt ring"];" is for Web applications? I am working on a windows app.
Question: How can I correct this?
Do I get this error because "string cnString = ConfigurationManager.AppSe
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>
Also, using this method is not specific to type [Windows Application vs. Web Application]
-saige-
-saige-
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.AppSe ttings["Co deSampleCS .Propertie s.Settings .NORTHWNDC onnectionS tring"];
If so, I am getting the same error.
Comment revised:
I am using: string cnString = ConfigurationManager.AppSe ttings["st rNorthWind "];
after I change the name attribute to "strNorthwind"
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.AppSe
If so, I am getting the same error.
Comment revised:
I am using: string cnString = ConfigurationManager.AppSe
after I change the name attribute to "strNorthwind"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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 -
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();
}
}
}
Should be:
CodeSampleCS.Properties.Se
Not:
ConnectionString
Additional information:
http://www.codeproject.com/Tips/416198/How-to-get-Connection-String-from-App-Config-in-Cs
-saige-