In my solution I have 4 projects named UI,Business,Data and common.In the Data project I have an app.config file with following values
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="LibrarySystemConnection" value="server=(local);Initial Catalog=SanasaLibrarySystem;Integrated Security=True;
User ID=sa;Password=123"/>
</appSettings>
</configuration>
In a class of the Data project I am accessing the above key as follows:
connectionString = ConfigurationSettings.AppSettings["LibrarySystemConnection"];
When I run the code the connectionString retuns null.Anybody have an idea what is the wrong in the code
You should move the app.config file to your primary UI project.
Either move the app.config file to the project which generates exe or read the config from the assembley using this
Related
I am unable to access CloudfigurationManager from within an method. The code was originally a klass library and I added a configuration file (App.config) where I am simply adding the values for a hard coded test for Azure SDK.
OpenSessionWithAzure()
private void OpenSessionWithAzure()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSetting["APP_SETTINGS"])// Visual Studio is unable to identify ConfigurationManager
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=userName;AccountKey=Key"/>
<add name="SasPolicyName" connectionString="myPolicy"/>
</connectionStrings>
</configuration>
Is there a reason why I am unable to access the ConfigurationManager within my application or a known workaround?
The reason for this error was due to the fact that I did not:
Have a reference to System.Configuration.dll in my class library.
Did not have the using System.Configuration
After adding performing the steps above I am not able to access ConfigurationManager.AppSettings["APP_SETTINGS"]
You need to add reference to System.Configuration and then add key-value pair in appSettings section
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SasPolicyName" value="myPolicy" />
<add key="APP_SETTINGS" value="MySetting" />
</appSettings>
<connectionStrings>
<add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=userName;AccountKey=Key"/>
</connectionStrings>
</configuration>
Then use like below,
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSetting["APP_SETTINGS"])
The method signature you use must be consistent with the setting in your App.config.
With the setting below,
<configuration>
<appSettings>
<add key="StorageConnectionString" value="XXXXXXXXX"/>
</appSettings>
</configuration>
use System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].
And System.Configuration.ConfigurationManager.ConnectionStrings["StorageConnectionString"] for
<configuration>
<connectionStrings>
<add name ="StorageConnectionString" connectionString="XXXXXXXXXX"/>
</connectionStrings>
</configuration>
When you attempt to read from appSettings, it reads from the .config file of the executing application. So in the simplest implementation you'd just need to add those configuration settings to that application's .config file (app.config or web.config.) There are other ways to approach it if you absolutely need the library to have its own .config file, but adding it to the executing application's .config is what's done most commonly.
I want to write and read my connection string in the Baglanti.cs class, but I don't know anything about this. I trying this first time. I search on Google, but I can't find any clear information.
The .NET way of doing this is using app.config file instead of .ini file
Create a new Application Configuration File with the following content
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="YourConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Retrieve the connectionString inside your application using
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Check out MSDN: Connection Strings and Configuration Files
I have a program called parser.exe and it uses a config file called parser.config (a txt format; I read it with streamreader). For some reasons C# complains and doesn't like this.
Unhandled Exception:
System.Configuration.ConfigurationErrorsException: Configuration
system failed to initialize --->
System.Configuration.ConfigurationErrors
BUT if I created a file called parser.exe.config. with just followng content application runs fine:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
Why this is happening and how to suppress this problem without having parser.exe.config and changing the config name from parser.config to parser.exe.config?
it uses a config file called parser.config (a txt format; I read it with streamreader)
There's no need for this. .NET already has a perfectly good framework for application configuration files. Just add an app.config file to your project and Visual Studio will automatically create a parser.exe.config file when you build.
You can then use the ConfigurationSettings.AppSettings dictionary to read individual configuration items - or use more complex structured for more complex configurations.
To use an external configuration file for a config section, just use the configSource attribute in app.config:
<configuration>
<appSettings configSource="parser.config" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
parser.config:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="Test" value="This is a test"/>
</appSettings>
why this is happening?
That's the way the framework was designed - it will look for a {executable name}.config file by default - you can add external config files as explained above but there's no way that I know of to have the framework look for a different file name by default.
You could load a new file into a separate configuration object:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = #"parser.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
But then you have to use that config instance to access config settings:
Console.WriteLine(config.AppSettings.Settings["test"].Value);
instead of
Console.WriteLine(ConfigurationManager.AppSettings["test"]);
But that seems like a long way to go to avoid the default config file name.
You can tell .NET to look for your settings in another file, like this:
<configuration>
<appSettings file="parser.config"></appSettings>
</configuration>
I have a class library application and am unable to configure a CONFIG file. My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
and the call in my application:
var test = ConfigurationManager.AppSettings["test"];
But when I run, always passes a null value.
Class libraries do not use a .config file. Only applications (app.config) and web site s(web.config) read the "config" file.
The class library can use the Configuration Manager to read a value, but they must running inside of an app/web site.
In ASP.Net rename 'app.config' to 'web.config' and place it in the root folder of web site.
You have to put the appSettings tag in the web.config of your website in order to use ConfigurationManager.AppSettings["test"] if you do not want to do that then make the setting xml file and read it in your library code.
Add this code to the web.confg
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
And this is get value
var test = WebConfigurationManager.AppSettings["test"];
If you are passing string you may use
in webconfig
<appSettings>
<add key="test" value="value test"/>
</appSettings>
In cs.
String update = Convert.ToString(ConfigurationManager.AppSettings["test "]);
thank u all!
I used my config from my web application and solved the problem. It was something simple that I was complicating.
I have an app.config file inside my TestProject, but when I try to read it using ConfigurationManager it reads from somewhere else and it's none of my app.config's. How to correct this?
Current config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Production" connectionString="Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"/>
</connectionStrings>
</configuration>
Current code:
ConfigurationManager.ConnectionStrings[0].ConnectionString
Expected output:
"Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"
Actual output:
"data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Try Referencing it by name.
ConfigurationManager.ConnectionStrings["Production"].ConnectionString
The config files automatically integrate machine.config which has that SQLEXPRESS connection string by default.