Answered by:
web.config in subfolders for websites

Question
-
User-1337876127 posted
I created a subfolder for my website and added a web.config file with the following:
<?xml version="1.0"?><configuration>
<connectionStrings>
<add name="CnTest" connectionString="Data Source=MyServer;Initial Catalog=MyDataBase;User Id=UserId;Password=Password;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>When I go to reference the connection string:
string cnTest = ConfigurationManager.ConnectionStrings["CnTest"].ConnectionString;
I get the following error:
Object reference not set to an instance of an object.
Is it because its a website and you can only have one web.config?
Here are the exact steps I did:
1. Right click on the project from the solution explorer, choose add-->new folder
2. Right click on the new folder created, choose add-->new item-->Web Configuration File
3. Now the folder contains a web.config
4. Add the connectionStrings section as shown above to the new file.
5. Assign the ConfigurationManager.ConnectionStrings["CnTest"].ConnectionString property to a string as shown above.
6. Receive error message Object reference not set to an instance of an object as shown above.
Thanks Before Hand,
AdielMonday, November 2, 2009 9:22 AM
Answers
-
User-1772898816 posted
Try this: http://www.codeproject.com/KB/aspnet/multipleWebConfig.aspx
If that doesn't work then try putting <clear /> in front of you connectionstring in your sub web.config.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 2, 2009 2:43 PM
All replies
-
User-1772898816 posted
I'm pretty sure that when you reference ConfigurationManager.ConnectionString it actually references the root web.config, not the one added in the subfolder.
Monday, November 2, 2009 11:19 AM -
User-1337876127 posted
Interesting because when you choose to create a new config file from the solution explorer, it automatically creates a blank <connectionStrings/> section for you. So then the question would be: how would you access the <connectionStrings> section that is in a web.config file stored in a subfolder?
Thanks Before Hand,
Adiel
Monday, November 2, 2009 2:33 PM -
User-1772898816 posted
Try this: http://www.codeproject.com/KB/aspnet/multipleWebConfig.aspx
If that doesn't work then try putting <clear /> in front of you connectionstring in your sub web.config.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 2, 2009 2:43 PM