0
votes

Cannot read connection string from web API configuration when debugging in Visual studio 2010

I am writing an ASP.net web API application and this is my first one. I am trying to set up my connection string and retrieve it using the configuration manager. There are three configuration files, web.config, web.debug.config and web.release.config. In each of these files I have added my desired connection string and have removed the default connection strings. But configuration manager still pulls up a default database connection string from someplace. Doing a global search through all the files in the solution there is no indication where configuration manager is reading this default string. How can I get my application to read the connection string that I specify in the configuration files? Why is configuration manager not reading any of these configuration files?

This is the string that I get back from configuration manager that I have no idea where it is getting it: data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

I am attempting to read the configuration string like this:
string CONNECT_STRING = ConfigurationManager.ConnectionStrings["Jobs"].ConnectionString;

3

3 Answers

0
votes

Try using this connection string

 <connectionStrings>
     <add name="Sqlcnn" providerName="SqlClient" connectionString="Data Source=Datasource name; user id=userid; password=pwd; database=DbName" />
 </connectionStrings>
0
votes

Try using the below connection. Other case the connection you are using ./SQLEXPRESS might not exist.


0
votes

When I encountered this problem I was running unit tests. For some reason when running unit tests the web API application does not read the connection string from the web config file. However, if I call the web API application from my client application the configuration string is read from the configuration file. I'm not exactly sure why the configuration file is not read during unit tests, but I'm sure I will figure it out in time.