I read many topics related to configuration manager but could not resolve the issue. I just want to read the connection string as well some appsetting keys from a CLASS LIBRARY in the web application.
I have reference to System.Configuration Class.
This is my code:
using System.Configuration;
...
string constr = ConfigurationManager.ConnectionStrings["cbuddydb"].ConnectionString;
string strUserName = ConfigurationManager.AppSettings["username"];
string strPwd = ConfigurationManager.AppSettings["password"];
But it seems reading from a different config file. not from the web.config in my project. Because the value read is wrong.
My web.config is below:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.data>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionstring=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient" password=""/>
</connectionStrings>
<appSettings >
<clear />
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
</system.data>
</configuration>
The reason for this is because of configuration file inheritance. The connection string at index 0 may not be in your config file, but it may have been inherited from machine.config etc. Have a look at ASP.Net config file hierarchy and inheritance: http://msdn.microsoft.com/en-us/library/ms178685.aspx
You could clear the inherited connection strings by specifying the following in your web.config
<connectionStrings>
<clear />
<add name=”MyConnString” connectionString=“Whatever“ />
</connectionStrings>
EDIT: In your config, place your connectionStrings and appSettings tags directly below the configuration element. They should not be within the system.data element. They are direct children of the configuration element. And remove the extra password attribute after the providerName. I can't validate your connection string, since I don't know how you're using it.
<configuration>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionString=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<appSettings >
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
<system.data>
....
You should consider encrypting sensitive information in your config file, like passwords.
Related
I have the following in my web.config located at the root of my project:
<configuration>
<connectionStrings>
<clear />
<add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connection Timeout=3000;" />
</connectionStrings>
<appSettings>
<add key="ConnectionString" value="test"/>
</appSettings>
....
I read from Startup.cs (this is an asp.net core web app):
string connection = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
However when I break on this, ConfigurationManager.ConnectionStrings and ConfigurationManager.AppSettings are empty (well, the first has some default connection string that is not the one in web.config).
What's going on here?
You will have to migrate the config to the new file appsettings.json
https://learn.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1
Not saying this is how you should do it, but you can do the following...
In ASP.Net Core 2.2, you should be able to add an XML configuration to IConfigurationBuilder using
configBuilder.AddXmlFile("app.config");
Contents is pretty much the same as above...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
You should then be able to access AppSettings/ConnectionStrings using...
ConfigurationManager.ConnectionStrings
ConfigurationManager.AppSettings.AllKeys
{string[1]}
[0]: "Test"
ConfigurationManager.AppSettings.Get("Test")
"TestValue"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=test.com,1234;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="serilog:write-to:MSSqlServer.connectionString" value="here reference the DefaultConnection connection string." />
</appSettings>
</configuration>
Same connection strings needs to be referenced in multiple places. For typo mistakes I would like to be able to reference a single connection string in the same file.
Is this possible?
I have asp.net mvc 4 project where try to use transform config, where have some default values and when I try to change to release mode some default values change to release values, but it isnt work. When I try to change to release mode and build application I have nothing changes. Here is my default values in Web.config:
<appSettings>
<add key="appId" value="####"/>
<add key="appSecret" value="####"/>
<add key="hostName" value="####"/>
</appSettings>
And here is what I have in my Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</appSettings>
</configuration>
Does anybody help me?
Using xdt:Locator="Match(name)", you're trying to match an attribute name which does not exist. You're looking for key for the appSettings:
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
I have a Web.config with several ConnectionStrings
<connectionStrings>
<add name="connStr1" connectionString="...
<add name="ConnStr2" connectionString="...
<add name="connStr3" connectionString="...
Is there a way using config transformations to remove a specific connectionstring? Something Like:
<connectionStrings>
<xdt:Remove connStr2?
Obviously no where near the correct syntax, but you get my drift...
This will remove a specific connection string based on its name.
<configuration>
<connectionStrings>
<add name="ConnStr2" xdt:Transform="Remove" xdt:Locator="Match(name)" connectionString=" " />
</connectionStrings>
</configuration>
Note that the connectionString value is not empty string, but is instead a space. Any non-empty value would do.
From the MSDN documentation on the subject:
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" />
</connectionStrings>
</configuration>
The Transform="Remove" is the magic you're looking for. There is also a Transform="RemoveAll" which you might be able to use in conjunction with a specific add(s).
EDIT
On second thought you may also be able to combine the Locator attribute with the Remove defined above to limit which elements you actually want to delete.
More definitively:
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[#name='ConnStr2'])" />
</connectionStrings>
</configuration>
Or similar should work.
I have a form which needs to get connected to SQL Server, and I have a drop down for selecting the list of databases and perform operations like primary key checking, etc.
But presently my connection string looks like this:
SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");
But apart from the given database, I need to take it variable, so that I can connect it to the database I select from the dropdown.
How can I do this?
Hmm you can declare your variables like this
<appSettings>
<add key="SmtpServerHost" value="********" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerUserName" value="******" />
<add key="SmtpServerPassword" value="*****" />
</appSettings>
and read like
string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);
I think he wants a "semi constant":
Web.Config
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<add name="YourName" providerName="System.Data.ProviderName" connectionString="Data Source={0}; Initial Catalog=myDataBase; User Id=myUsername; Password=myPassword;" />
</connectionStrings>
</configuration>
CS file
String Servername = "Test";
String ConnectionString = String.Format(ConfigurationManager.ConnectionStrings["YourName"].ConnectionString, ServerName);
you can use the connectionStrings tag in the app.config configuration. You can add as many as you want (giving them each a separate key) and then retrieve them
example app.config xml (set providerName to a valid provider, for example System.Data.SqlClient, and the appropriate connection string) :
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="firstDb"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
<add name="secondDb"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
example on getting them and listing them (in your case, you would create the appropriate items in the dropdown and set the values) :
ConnectionStringSettingsCollection settings =
ConfigurationManager.ConnectionStrings;
if (settings != null)
{
foreach(ConnectionStringSettings cs in settings)
{
Console.WriteLine(cs.Name);
Console.WriteLine(cs.ProviderName);
Console.WriteLine(cs.ConnectionString);
}
}
You could use the AppSettings section. Read here for an example.