I am using VS2012. I have to keep connection string in app.config and have to access it from my cs file. But I am unable to do it in VS2012. Following is what I have found from net but I think it works on earlier version of VS not on VS2012.
app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="DataFormConnection"
connectionString="Data Source=abcdd;database=xyz;uid=4566;pwd=987"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
How I am accessing it:
connectionString = ConfigurationManager.ConnectionStrings["DataFormConnection"].ConnectionString;
Getting error: type or name does not exist in System.Configuration.ConfiguarationSettings
Go to references, and add a reference to System.Configuration.
Go to References, and add a reference to System.Configuration
Once you have done this, you should be able to reference System.Configuration.ConfigurationManager.
Related
I'm having some difficulties to connect to SQL Server running on a virtual machine (virtualbox). I'm using ADO.NET in C# to do the connection through a connection string but I'm not having any success with this.
My app.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionStrings>
<add name="AP3"
connectionString="Data Source=190.166.2.8,1433;Initial Catalog=AP3;
User Id=sa;Password=PasswOrd;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I don't know if I'm doing anything wrong...
It seems that you've not allowed Sql Server for Remote Connections. Kindly follow this link for more details
You can add DBMSSOCN to use TCP/IP instead of Named Pipes.Maybe it is a problem.
try this connection string.
"Data Source=190.166.2.8,1433;Network Library=DBMSSOCN;Initial Catalog=AP3; User Id=sa;Password=PasswOrd;"
My application runs only on .NET 2.0.
But, I had the following text in App.config in the deployed application:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="assemblies"
type="Simple.Framework.AssembliesConfigurationSection, Simple.Framework"/>
</configSections>
<connectionStrings>
<add name="SystemSqlServer"
connectionString="Data Source=.\sqlexpress;Initial Catalog=gre;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
I replaced the supportedRuntime with the following text, as my application took a long time to start:
<supportedRuntime version="v2.0"
sku=".NETFramework,Version=v2.0"/>
But, I am getting the following message:
The following picture shows that .NET 2.0 is already installed:
and, my .net 3.5.1 is already turned on, and there is no additional option for .net 2.0.
The problem is that the table adapter keeps referencing a connection string that I have not set up for it. When I go to each data table in the DataSet Designer, the connect says "MyConnectionString(settings)". When I search for the incorrect connection string, VS can't find it.
The project that is reused over multiple solutions. I have three configurations: Debug, Staging and Release. Each configuration has it's own connection string. My app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connect.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
Each configuration file looks something like this:
<connectionStrings>
<clear/>
<add name="Properties.Settings.MyConnectionString" connectionString="Data Source=CorrectDataSourceforthisConfig\SQL;Initial Catalog=MyDB;Trusted_Connection=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In my dataset, I have this XML:
<Connections>
<Connection AppSettingsObjectName="Settings" AppSettingsPropertyName="MyConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyConnectionString (Settings)" ParameterPrefix="#" PropertyReference="ApplicationSettings.MyMenu.Properties.Settings.GlobalReference.Default.MyConnectionString" Provider="System.Data.SqlClient" />
</Connections>
In my settings.designer.cs, I have this:
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=CorrectDataSourceForDebug\SQL;Initial Catalog=MyDB;Integrated Security=True")]
public string RMSConnectionString {
get {
return ((string)(this["MyConnectionString"]));
}
}
Where is this rogue connection string coming from? Any help, ideas, advice and opinions would be greatly appreciated.
The connection string is stored in your app.config file as well as in your sometimes in your dataset and sometimes in your code. In my case, I was able to fix this problem by going into Explorer and deleting all the files that I had accidentally created (i.e. Form1) and by searching my solution and making sure tha there were no reference to the incorrect connection string. Then I deleted all instances of the .DLL that I had used when I included this project in different solutions and re-referenced and rebuilt all the projects.
There is also machine.config which is the master configuration file on your system. This may be where your hidden connection string is stored.
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files
The machine.config file also contains a connectionStrings section, which contains connection strings used by Visual Studio. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.
I have a console application and in the App.config I need to add a connectionString to another SQL server that is in the same network.
If I try the connectionString to a local server by only passing the Server='localhost' its worked but I cannot make it work for an outside server.
Thanks
Here is the connection file
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<connectionStrings>
<add connectionString="Server=LocalServer;database=DAtaBase;user=UserName;pwd=Password" name="Connection"/>
</connectionStrings>
</configuration>
Example connection to outer server:
<connectionStrings>
<add name="Namespace.Settings.outerSQL" connectionString="Data Source=192.168.0.100\SQLEXPRESS;Initial Catalog=database_name;User ID=user;Password=password"/>
</connectionStrings>
You need address to remote server and credentials provided (if it's not Windows auth, for which you use "Integrated Security").
inside of a app.config / web.Config file you would have the following and this is a simple and easy way to connect to a sql server database.. also confirm what database you are using because the connection string can be different depending on the DBMS
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DbConn" connectionString="Data Source=the name of your database;User Id=UserName;Password=Password;"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
I had configuration warning in app.config of my wpf application.
my entire app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="schema URL">
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I tried :
How to remove warning 'The 'configuration' element is not declared.' Visual Studio C#
and
The configuration element is not declared
and as stated in one of answers, tried to restart VS and start again. No luck
The configuration warning is gone, however the application can't start
Use this App.config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>