App.config seems to be ignored - c#

I've this Class Library, as a result of a refactor action.
I added an App.config file and added something like this:
<configuration>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when I run the application, debugging learns me this is totally ignored. The immediate window tells me:
ConfigurationManager.ConnectionStrings[0]
{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Key: "LocalSqlServer"
Name: "LocalSqlServer"
Properties: {System.Configuration.ConfigurationPropertyCollection}
ProviderName: "System.Data.SqlClient"
I've checked generated config file in the bin directory and its contents are identical to the App.config.
I try to read the App.config using:
ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString
Nothing out of the ordinary I'd say, but what is going wrong?

A class library doesn't get its own config; for an app named Foo.exe you need your configuration to be in Foo.exe.config. The exception here is web apps, where web.config is the naming convention.

This connectionString should be in the corresponding app.config of the exe that you are running.

Is the file being copied to output? Secondly the connection string should be accessible from the AssemblyName.Settings.Properties class which is generated from your code.

Related

Connection String null reference exception in other systems

I need to set .dbml connection string because it's change itself after deploying.
I test many posts in google but no one works for me.
here that links I already test in my app.
1. Setting Connection string in dbml files using Linq to SQL in Visual Studio 2008
2. Point connectionstring in dbml to app.config
in my app code, my connection string is
<add name="PureWater.Properties.Settings.purewaterConnectionS"
connectionString="Data Source=.;Initial Catalog=purewater;Integrated Security=True"
providerName="System.Data.SqlClient" />
According to 1 and 2 abbove, I set .dbml connection none and left empty. Then I modify .dbml designer code.
after that, I call data context (dbml) from other windows forms like this:
pureWaterCxtDataContext cxt = new pureWaterCxtDataContext(ConfigurationManager.ConnectionStrings["PureWater.Properties.Settings.purewaterConnectionS"].ConnectionString);
Everything is ok and I don't have error on my own system but I get an error on other systems on ConfigurationManager.ConnectionStrings that it is a null reference. This is very odd and can't trace exe file in other system to get what's happening?
Thanks in advance for your help
Update:
my app.config file is here:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<configSections>
</configSections>
<connectionStrings>
<add name="PureWater.Properties.Settings.purewaterConnectionS"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=purewaterPartovi; Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

I have two web.config files. Which one should I use?

I recently built an application using visual studio express. This application was built using a local DB provided by VS express. I now have a subscription (free trial) to Microsoft Azure. What changes do I need to make to my project in order to point my project to the newly created Azure DB?
I also noticed I have 2 web.config files. one in the root directory and one in the views folder.
I believe I have to make an update to my connection string, but in a specific config file or both?
Here is my root directory web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20160922101748.mdf;Initial Catalog=aspnet-MvcMovie-20160922101748;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and my (views) web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Any help would be highly appreciated.
I believe only one web.config with connection strings should be enough. Keep the one in the root.
Download Azure DB connection strings from the Azure portal and replace connectionString values in the web.config file with the Azure DB connection string(s).
You might also need to whitelist your machine's IP address in the Azure Portal to allow connections to Azure DB.
You only need to change the web.config on the root folder as thats the one used by the application.
And you are right, you need to point the connection string to your Azure database.
You definitely only need to update the one web.config in the root of the application with the connection string. The other web.config in the views folder is needed for your views to use html helpers and to make routing work as you expect in an mvc application so do not remove it.

ConfigurationManager does not point to correct app.config

I am trying to use ConfigurationManager to get the connection string from a project call FileShareAccessLibrary.
This is the code I am writting in order to do this:
ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ConnectionString
This is the content of app.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="FileShareAccessLibrary" connectionString="......"
providerName="System.Data.SqlClient" />
<add name="FileShareAccessLibrary.Properties.Settings"
connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
When I run my app I get a NullReferenceException because ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"] returns null.
While debugging I noticed that none of the two connection strings are stored inside ConfigurationManager.ConnectionStrings so I figured that the ConfigurationManager is pointing to another file.
In my project I have not other app.config file.
Is there something I am doing wrong here?
Why is ConfigurationManager not getting my connection string?
If your FileShareAccessLibrary project is a class library rather than a windows forms application or console application then you will need to move the connection strings (and app settings if you have any) from the FileShareAccessLibrary config file to the config file of the application(s) that reference the FileShareAccessLibrary dll.

Connect to a MSSQL DB using Web.config

I have created a ASP.NET C# MVC project. I am trying to connect it to a MSSQL DB. In the Web.config file i added the following;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\o\documents\visual studio 2012\Projects\ppl\ppl\App_Data\ppldb.mdf";Integrated Security=True" />
</connectionStrings>
But, the above code is incorrect ; I want it to be as follows;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|ppldb.mdf;Integrated Security=True" />
</connectionStrings>
But What is DataDirectory ? Where do i specify it ?
What is name="sdbconnection" ? Can i give any name that i want ?
First question
Quoting MSDN:
The |DataDirectory| portion of the connection string specifies that
the MDF file is located inthe App_Data directory
Moreover you can change it using following function:
AppDomain.CurrentDomain.SetData("DataDirectory", "D:\database");
Second question
name="sdbconnection" allows you to use specified conection string. So in your code you can write:
string connectionString = ConfigurationManager.ConnectionStrings["sdbconnection"].ConnectionString;
to acces this connection string
Name is use to identify and use code your applications
Please refer this article http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
Data Directory is usually App_Data folder and in web.config file name is use to use connection string in the application
Ex:String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].ConnectionString;
or
String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].toString();

.NET 4: How to configure EDMX file in other assembly in Web.Config

I have a problem with configuring an EDMX file that lives in an other assembly than by web project. My project looks somewhat like this:
Project 1
--> Database.edmx
--> App.Config
Project 2
--> Ton's of .cs and .aspx files.
--> Web.Config with the proper connection string.
Inside Visual Studio the updating of the .EDMX file inside Project 1 goes smoothly and while I had the .EDMX file inside project 2 the application ran as it supposed to.
Anyone has an idea on how to configure the .EDMX file inside Project 1 to point to the connectionstring of Web.Config? (or should I use Project1.dll.config to configure Project 1?)
You have to change the * in the connection string for the assembly name where the .edmx files are in:
<add name="Entities" connectionString="metadata=res://*/Models.EF.Model.csdl|res://*/Models.EF.Model.ssdl|res://*/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />
for
<add name="Entities" connectionString="metadata=res://Project2/Models.EF.Model.csdl|res://Project2/Models.EF.Model.ssdl|res://Project2/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />
As it turned out, there were 2 problems. One was solved replacing the * in the connection string.
The second problem was the one described here: http://blogs.teamb.com/craigstuntz/2010/08/13/38628/
It had to do with the path .csdl, .ssdl and .msl files had as resources inside the Project1 assembly
Anyway, things function properly now
Easier way is to take connection string from Web.Config and copy them into App.Config and point EDMX's connectionstring to same DB information. e.g
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="aspnetdbEntities" connectionString="metadata=res://*/Data.PSBData.csdl|res://*/Data.PSBData.ssdl|res://*/Data.PSBData.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\aspnetdb.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Also you need to check if the namespaces are correct if you have moved Database.edmx from Project 2 to Project 1, which you can check by opening Database.edmx and goto code behind.

Categories