I have replaced the bottom line with the code that is in comments to make the location dynamic but it doesn't work. I have also changed the app.config file accordingly but still no hope. Could someone please guide me.
// public string str = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\b5012622\Desktop\Jan_Prototype\DB.mdf;Integrated Security=True";
public string str = ConfigurationManager.ConnectionStrings["connectionstrings"].ConnectionString;
Here is the app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionstrings>
<add name="dbConnection">
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\DB.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"
</add>
</connectionstrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
You should set your connectionstring as attribute of add element instead of the value of add element
Put proper connection string name mentioned at name attribute in connectionstrings element:
public string str = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
Related
This question already has answers here:
How to use double quotes in app.config
(2 answers)
Closed 4 years ago.
I have the app.config file which contains the following code for my Windows forms application.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
<connectionStrings>
<add name = "myConnection"
connectionString = "Data Source=(LocalDB)\v11.0; AttachDbFilename = "C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf";Integrated Security=True"
providerName = "System.Data.SqlClient" />
</connectionStrings>
</configuration>
connectionString attribute is giving error. Using \\ instead of \ doesn't help. It states that white space required.
How can I fix this?
Internal quotes need to be escaped with ":
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
<connectionStrings>
<add name = "myConnection"
connectionString = "Data Source=(LocalDB)\v11.0;"AttachDbFilename="C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf"; IntegratedSecurity=True"
providerName = "System.Data.SqlClient" />
</connectionStrings>
</configuration>
The issue is that the XML parser figured that your connection string ended after the \v11.0;", but it continues on and you get other errors. You have to remove the internal quotes with the " escape sequence.
I got mixed with app.config file.
I had config that failed to get connectionStrings:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<connectionStrings>
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ProgrammingInCSharpConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
</connectionStrings>
</configSections>
</configuration>
Problem was solved after placing <connectionStrings> outside of <connectionStrings> :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections />
<connectionStrings>
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ProgrammingInCSharpConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
</connectionStrings>
</configuration>
But I found many samples with pattern below
<configSections>
<connectionStrings>
...
</connectionStrings>
</configSections>
It might work in some cases?
What is logic of configuration file in general and what kind of information might be located in each section?
No. It is not possible to have a valid app.config file and yet having a connectionStrings in your configSections node.
According to MSDN, this is the list of allowed elements:
section
sectionGroup
add
clear
connectionStrings isn't in the list and I don't expect an application to find the connection strings in that place.
The configSections node contains configuration section per namespace declarations, but is not usually used for connection strings, unless they are namespace - specific.
But I found many samples with pattern below
The patterns you found could never work since <connectionStrings> is not allowed to be nested directly under <configSections>.
This could be achieved, by stating a connection string variable under the <section> itself, and if the server code targets them via a specific namespace.
An example:
<configuration>
<configSections>
<section name="section1" type="System.MyNameSpace" />
</configSections>
<section1 connString="...conn.." />
</configuration>
In the example above the namespace System.MyNameSpace will now have a variable (field) called connString, so it can target it in code behind.
For conclusion, if your connection strings are not per namespace, the <connectionStrings> element should be nested directly under the <configuration> node and no where else.
Here is a snippet of my app.config file.
<MyProject>
<ConnectionStrings>
...
<Operational providerType="SqlServer" provider="" dataSource="<ServerName>" initialCatalog="<DBName>" security="" userName="<SomeUser>" password="<Password>" />
...
</ConnectionStrings>
</MyProject>
In my C# code, I have this value available in some variable, like...
string OperationalConnectionConfiurationPath = "MyProject/ConnectionStrings/Operational";
How can I get my SqlConnection object out of this?
connectionStrings are a child of the configuration section. Get rid of that <MyProject>stuff.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Operational" connectionString="Data Source=SERVER_NAME;Initial Catalog=DB_NAME;User Id=USER_ID;Password=PASSWORD;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You can get the connection string by:
using System.Configuration;
var connectionString = ConfigurationManager.ConnectionStrings["Operational"].ConnectionString;
I am making a windows service.
It contain one App.config file as below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=192.168.1.116;Initial Catalog=Conezone;User Id=sa;Password=saadmin#123;"/>
<add key="FilePath" value="D:\Autoparts Guru\LINES2\"/>
</appSettings>
</configuration>
To get FilePath value in service code, I am writing
ConfigurationManager.AppSettings["FilePath"].ToString()
But it generates NullReferenceException.
Hope i get the answer quickly.
This code may help you:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=000.000.000.000;Initial Catalog=mydb;User Id=sa;Password=sa;"/>
<add key="FilePath" value="myfilepath"/>
</appSettings>
</configuration>
Namespace
using System.Configuration;
Code:
string filepath = string.Empty;
filepath = ConfigurationManager.AppSettings["FilePath"];
I'm doing a c# course at university, and now I'm beginning linQ to xml, linQ to Sql-Server, etc.
I work with the example projects in two PCs (university PC and office PC)
Is there an easy way to change connection string (from app.config) at runtime, or designtime (using a constant in main.cs) so I can use a connection string at university, and a connection string at the office easily?
Thanks a lot in advance,
You could try something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Environment" value="Home"/>
</appSettings>
<connectionStrings>
<add name="Work" connectionString="..."/>
<add name="Home" connectionString="..."/>
</connectionStrings>
</configuration>
and, later:
string environment = ConfigurationManager.AppSettings["Environment"];
ConfigurationManager.ConnectionStrings[environment].ConnectionString;
Another way of doing it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="HomeEnvironment"
connectionString="Data Source=**HOME-COMPUTER**\SQLEXPRESS;Initial Catalog=**HomeDatabase**;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<add name="WorkEnvironment"
connectionString="DataSource=**WORK-COMPUTER**\SQLEXPRESS;Initial Catalog=**WorkDatabase**;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
and use it like this:
var environment = Environment.MachineName == "HOME-COMPUTER" ? "HomeEnvironment" : "WorkEnvironment";
var connectionString = ConfigurationManager.ConnectionStrings[environment].ConnectionString;
var dbContext = new **Databasename**ContextDataContext(connectionString);
Bold strings should be customised as required