Access web.config from lower folder?

J

Jason Chan

I have a subfolder in a webapp

How can the aspx in the subfolder access the web.config in the webapp.

I.e. I stored the connectionstring in the \webapp\web.config
how the \webapp\subfolder\default.aspx extract the connection?

ConfigurationSettings.AppSettings["ConnectionString"] return empty string

Do I have to copy the web.config to the subfolder?
 
J

Jason Chan

seem that the ConfigurationSettings.AppSettings["ConnectionString"] is not
the correct statment to extract the string in asp.net 2.0, becos I cannot do
it even in webapp

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=...."
ProviderName="System.Data.SqlClient"/>
</connectionStrings>


"Jason Chan" <[email protected]> ¼¶¼g©ó¶l¥ó·s»D:%[email protected]...
I have a subfolder in a webapp

How can the aspx in the subfolder access the web.config in the webapp.

I.e. I stored the connectionstring in the \webapp\web.config
how the \webapp\subfolder\default.aspx extract the connection?

ConfigurationSettings.AppSettings["ConnectionString"] return empty string

Do I have to copy the web.config to the subfolder?
 
G

gadget_guru

no, web.config should be in the root folder of the app.
The way you mention should work - does
ConfigurationSettings.AppSettings["ConnectionString"] work in the
rootfolder ? (maybe something is wrong in the syntax of web.config)
 
J

Juan T. Llibre

re:
How can the aspx in the subfolder access the web.config in the webapp.

It does it without having to do anything special.

re:
Do I have to copy the web.config to the subfolder?

No, you don't.

Check your syntax.

If you want to, post the web.config appSettings section
( change any sensitive information, like passwords, if any )
and the line of code which you are using to retrieve the info.

string connect = ConfigurationSettings.AppSettings["ConnectionString"];
returns empty and your web.config's appSettings have a key like :

<appSettings>
<add key="ConnectionString" value="SomeConnectionStringtoYourDB" />
</appSettings>

?




Jason Chan said:
I have a subfolder in a webapp

How can the aspx in the subfolder access the web.config in the webapp.

I.e. I stored the connectionstring in the \webapp\web.config
how the \webapp\subfolder\default.aspx extract the connection?

ConfigurationSettings.AppSettings["ConnectionString"] return empty string

Do I have to copy the web.config to the subfolder?
 
J

Juan T. Llibre

You should have said you were using ASP.NET 2.0 from the start.

Actually, ConfigurationSettings.AppSettings["ConnectionString"]
WILL return data from an <appSettings...> section which does
*not* have the <connectionStrings> section.

If you *do* include a <connectionStrings> section, then you can use :

ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

or

String connString =
ConfigurationManager.ConnectionStrings(ConnectionString).ConnectionString
 
J

Jason Chan

thanks , it work
"Juan T. Llibre" <[email protected]> ¼¶¼g©ó¶l¥ó·s»D:%[email protected]...
You should have said you were using ASP.NET 2.0 from the start.

Actually, ConfigurationSettings.AppSettings["ConnectionString"]
WILL return data from an <appSettings...> section which does
*not* have the <connectionStrings> section.

If you *do* include a <connectionStrings> section, then you can use :

ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

or

String connString =
ConfigurationManager.ConnectionStrings(ConnectionString).ConnectionString
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top