Saturday, March 24, 2012

web.config question 2.0

I have a web.config in my application that contains the connection strings
to all my datasources. I want to move these connection strings to another
web config up the folder hierarchy so that all my apps can use the same
connection strings. That is supposed to be how it's done, no? Instead of
the web.config being in c:\inetpub\wwwroot\myApp\web.config, I have it in
c:\inetpub\wwwroot\web.config. However, I get an "Object reference not set
to an instance of an object" error when I do this. This used to work fine
in Visual Studio 2003 .net 1.0.
My connection string is fine:
<connectionStrings>
<add name="myDB" connectionString="Data Source=mySvr;Initial
Catalog=myDB;Integrated Security=True "/>
</connectionStrings>
And this is how I retrieve in in the web app:
_sSQLConn =
System.Configuration.ConfigurationManager.ConnectionStrings("myDB").ToString
What am I doing wrong?
Thanks for your help.Try using the complete path for the Data Source section of your
connectionstring, because you are using a relative Data Source changing the
location of your Web.config might cause a problem.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"eagle" <eagletender2001@.yahoo.com> wrote in message
news:OpmrRvjxHHA.536@.TK2MSFTNGP06.phx.gbl...
>I have a web.config in my application that contains the connection strings
> to all my datasources. I want to move these connection strings to another
> web config up the folder hierarchy so that all my apps can use the same
> connection strings. That is supposed to be how it's done, no? Instead of
> the web.config being in c:\inetpub\wwwroot\myApp\web.config, I have it in
> c:\inetpub\wwwroot\web.config. However, I get an "Object reference not
> set
> to an instance of an object" error when I do this. This used to work fine
> in Visual Studio 2003 .net 1.0.
> My connection string is fine:
> <connectionStrings>
> <add name="myDB" connectionString="Data Source=mySvr;Initial
> Catalog=myDB;Integrated Security=True "/>
> </connectionStrings>
> And this is how I retrieve in in the web app:
> _sSQLConn =
> System.Configuration.ConfigurationManager.ConnectionStrings("myDB").ToStri
ng
> What am I doing wrong?
> Thanks for your help.
>
>
re:
!> I want to move these connection strings to another web config up the
!> folder hierarchy so that all my apps can use the same connection strings.
Yes, you can do that.
re:
!> That is supposed to be how it's done, no?
Not sure if that's how it's *supposed* to be, but it *can* be done.
I suppose it's a developer choice whether you do that, or not,
or whether you store each app's connection strings in each app's web.config.
re:
!>My connection string is fine:
!> <connectionStrings>
!> <add name="myDB" connectionString="Data Source=mySvr;Initial
!> Catalog=myDB;Integrated Security=True "/>
!> </connectionStrings>
Yes, that's fine...for a connection string in your app's web.config,'
but not for a web.config up the folder hierarchy.
re:
!> What am I doing wrong?
You are not configuring the root web.config as the one to retrieve the value
from.
Try this :
Dim configPath As String = "/"
' this gets the root web.config
Dim config As ConnectionStringsSection = WebConfigurationManager.GetWebAppli
cationSection("connectionStrings")
' this declares the connectionStrings section as the one you want to retriev
e the value from
Dim value as String = "The configured connectionStrings connection is : " &
config.connectionstrings("myDB").ToString()
' this retrieves the value for the connection string "myDB"
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"eagle" <eagletender2001@.yahoo.com> wrote in message news:OpmrRvjxHHA.536@.TK2MSFTNGP06.phx.
gbl...
>I have a web.config in my application that contains the connection strings
> to all my datasources. I want to move these connection strings to another
> web config up the folder hierarchy so that all my apps can use the same
> connection strings. That is supposed to be how it's done, no? Instead of
> the web.config being in c:\inetpub\wwwroot\myApp\web.config, I have it in
> c:\inetpub\wwwroot\web.config. However, I get an "Object reference not se
t
> to an instance of an object" error when I do this. This used to work fine
> in Visual Studio 2003 .net 1.0.
> My connection string is fine:
> <connectionStrings>
> <add name="myDB" connectionString="Data Source=mySvr;Initial
> Catalog=myDB;Integrated Security=True "/>
> </connectionStrings>
> And this is how I retrieve in in the web app:
> _sSQLConn =
> System.Configuration.ConfigurationManager.ConnectionStrings("myDB").ToStri
ng
> What am I doing wrong?
> Thanks for your help.
>
>
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:O6yzjKmxHHA.3784@.TK2MSFTNGP02.phx.gbl...

> I suppose it's a developer choice whether you do that, or not,
> or whether you store each app's connection strings in each app's
> web.config.
I always go for the latter...

> Dim configPath As String = "/"
> Dim config As ConnectionStringsSection =
> WebConfigurationManager.GetWebApplicationSection("connectionStrings")
> Dim value as String = "The configured connectionStrings connection is : "
> & config.connectionstrings("myDB").ToString()
Are you sure about this? You're declaring a configPath variable to point to
the root web.config, but then not actually using it anywhere...
Mark Rae
ASP.NET MVP
http://www.markrae.net
re:
!> I always go for the latter...
Indeed, that's my preference, too.
I supplied the alternative only because the OP requested the solution
for accessing a root web.config from an application down the wwwroot hierarc
hy.
There's an additional caveat about storing all the connection strings in the
root.
You can't mix 1.1 and 2.0 apps in your site.
You *must* have either all apps being 2.0, or all apps being 1.1.
Mixing 1.1 and 2.0 apps will bring the inevitable "configuration section not
recognized" error.
re:
!> Are you sure about this?
Try it... ;-)
...but only if you don't mix 1.1 and 2.0 apps in your wwwroot hierarchy.
re;
!> You're declaring a configPath variable to point to
!> the root web.config, but then not actually using it anywhere...
Here's what happened...
I tried using :
Dim configPath As String = "/"
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(c
onfigPath)
Dim config2 As ConnectionStringsSection = WebConfigurationManager.GetWebAppl
icationSection("connectionStrings")
Dim value as String = "The configured connectionStrings connection is : " &
config2.connectionstrings("myDB").ToString()
...but when I removed
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(c
onfigPath)
...and used :
Dim configPath As String = "/"
Dim config As ConnectionStringsSection = WebConfigurationManager.GetWebAppli
cationSection("connectionStrings")
Dim value as String = "The configured connectionStrings connection is : " &
config.connectionstrings("myDB").ToString()
...it *still* worked.
I did some more digging, after I posted the solution, and the answer is that
,
if configPath is a null reference, the root Web.config file is opened!
So, actually, Dim configPath As String = "/" is *not* needed if you want to
open the root web.config.
If retrieving a web.config in *any* virtual directory, you'll need to use :
Dim configPath As String = "/someVirtualApp"
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(c
onfigPath)
Dim config2 As ConnectionStringsSection = WebConfigurationManager.GetWebAppl
icationSection("connectionStrings")
Dim value as String = "The configured connectionStrings connection is : " &
config2.connectionstrings("myDB").ToString()
...that will open the web.config in /someVirtualApp and retrieve the "myDB"
connection string.
If you just use :
Dim config As ConnectionStringsSection = WebConfigurationManager.GetWebAppli
cationSection("connectionStrings")
Dim value as String = "The configured connectionStrings connection is : " &
config.connectionstrings("myDB").ToString()
...it will open the root web.config and retrieve the "myDB" connection stri
ng.
Finally, if you use :
Dim configPath As String = ""
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(c
onfigPath)
Dim config2 As ConnectionStringsSection = WebConfigurationManager.GetWebAppl
icationSection("connectionStrings")
Dim value as String = "The configured connectionStrings connection is : " &
config2.connectionstrings("myDB").ToString()
...it will retrieve the current app's web.config and retrieve the "myDB" co
nnection string.
Of course, there's easier ways of accessing the current app's web.config.
These contortions are only needed when accessing
a web.config in a different application, or in the root.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Mark Rae [MVP]" <mark@.markNOSPAMrae.net> wrote in message news:erFFCsmxHHA.3536@.TK2MSFTNGP03.ph
x.gbl...
> "Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message news:O6yzjKm
xHHA.3784@.TK2MSFTNGP02.phx.gbl...
>
> I always go for the latter...
>
> Are you sure about this? You're declaring a configPath variable to point t
o the root web.config, but then not actually
> using it anywhere...
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:ev3xaGnxHHA.1184@.TK2MSFTNGP04.phx.gbl...

> You can't mix 1.1 and 2.0 apps in your site.
> You *must* have either all apps being 2.0, or all apps being 1.1.
> Mixing 1.1 and 2.0 apps will bring the inevitable "configuration section
> not recognized" error.
Ah yes - it's all coming back to me now...

> I did some more digging, after I posted the solution, and the answer is
> that,
> if configPath is a null reference, the root Web.config file is opened!
Ah - I didn't know that...

> Dim configPath As String = "/someVirtualApp"
> Dim config As Configuration =
> WebConfigurationManager.OpenWebConfiguration(configPath)
> Dim config2 As ConnectionStringsSection =
> WebConfigurationManager.GetWebApplicationSection("connectionStrings")
> Dim value as String = "The configured connectionStrings connection is : "
> & config2.connectionstrings("myDB").ToString()
That's what I was expecting to see...

> Of course, there's easier ways of accessing the current app's web.config.
> These contortions are only needed when accessing
> a web.config in a different application, or in the root.
Indeed. Because of all the hoops you have to jump through, I guess you've
got to have a *really* good reason for wanting to do this...
Mark Rae
ASP.NET MVP
http://www.markrae.net
The problem you may have here is that if the folder containing your
web.config is marked as an application in IIS, that's the web.config that
takes precedence for this folder. If you were to undo the application here,
remove the web.config, and ensure that all the required items are in the one
in the parent folder, then it may work.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"eagle" wrote:

> I have a web.config in my application that contains the connection strings
> to all my datasources. I want to move these connection strings to another
> web config up the folder hierarchy so that all my apps can use the same
> connection strings. That is supposed to be how it's done, no? Instead of
> the web.config being in c:\inetpub\wwwroot\myApp\web.config, I have it in
> c:\inetpub\wwwroot\web.config. However, I get an "Object reference not se
t
> to an instance of an object" error when I do this. This used to work fine
> in Visual Studio 2003 .net 1.0.
> My connection string is fine:
> <connectionStrings>
> <add name="myDB" connectionString="Data Source=mySvr;Initial
> Catalog=myDB;Integrated Security=True "/>
> </connectionStrings>
> And this is how I retrieve in in the web app:
> _sSQLConn =
> System.Configuration.ConfigurationManager.ConnectionStrings("myDB").ToStri
ng
> What am I doing wrong?
> Thanks for your help.
>
>
re:
!> If you were to undo the application here, remove the web.config, and ensu
re
!> that all the required items are in the one in the parent folder, then it
may work.
...unless there's web applications running more than one version of the .Ne
t Framework.
In that case, it's a shifty proposition, given that there's incompatible ver
sions
of some web.config properties which will crash when implemented.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in messa
ge
news:E8D2E8E3-D8E6-4F7D-B0C0-835F76269F35@.microsoft.com...
> The problem you may have here is that if the folder containing your
> web.config is marked as an application in IIS, that's the web.config that
> takes precedence for this folder. If you were to undo the application here
,
> remove the web.config, and ensure that all the required items are in the o
ne
> in the parent folder, then it may work.
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> BlogMetaFinder(BETA): http://www.blogmetafinder.com
>
> "eagle" wrote:
>
I should have added, Peter, that System.Configuration.ConfigurationManager
can only retrieve connection strings from within the current application.
To retrieve configuration settings from a different application, you need :
WebConfigurationManager.OpenWebConfiguration.
What you're proposing would remove the application's configuration as an app
lication,
which might be troublesome for some application properties, if they're confi
gured in the root application.
See my replies to Mark, in this same thread, for more explanatory concepts..
.and sample code.
The bottom-line answer is : always use the *current* application's
web.config to store/retrieve connection strings.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in messa
ge
news:E8D2E8E3-D8E6-4F7D-B0C0-835F76269F35@.microsoft.com...
> The problem you may have here is that if the folder containing your
> web.config is marked as an application in IIS, that's the web.config that
> takes precedence for this folder. If you were to undo the application here
,
> remove the web.config, and ensure that all the required items are in the o
ne
> in the parent folder, then it may work.
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> BlogMetaFinder(BETA): http://www.blogmetafinder.com
>
> "eagle" wrote:
>
Good point. Agreed.
:-)
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Juan T. Llibre" wrote:

> I should have added, Peter, that System.Configuration.ConfigurationManager
> can only retrieve connection strings from within the current application.
> To retrieve configuration settings from a different application, you need
:
> WebConfigurationManager.OpenWebConfiguration.
> What you're proposing would remove the application's configuration as an a
pplication,
> which might be troublesome for some application properties, if they're con
figured in the root application.
> See my replies to Mark, in this same thread, for more explanatory concepts
..and sample code.
> The bottom-line answer is : always use the *current* application's
> web.config to store/retrieve connection strings.
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espa?ol : http://asp.net.do/foros/
> ======================================
> "Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in mes
sage
> news:E8D2E8E3-D8E6-4F7D-B0C0-835F76269F35@.microsoft.com...
>
>

0 comments:

Post a Comment