How to retreve connection string programmaticaly from this

D

dbuchanan

Here is part of the web.config - it is given to me like this.
=======================
<configuration>
<appSettings/>
<connectionStrings>
<add name="MyConnectionName" connectionString="Data
Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
=======================
How do I get the connection string?

The follwoing code is my attempt to connect

string cnStr;
cnStr = ConfigurationSettings. << At this point intellisense gives me the
following:

AppSettings
Equals
GetConfig
ReferencEquals

Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
code

cnStr =
ConfigurationSettings.connecionStrings["MyConnectionName"].ConnectionString;

However I get the following error
Error 1 'System.Configuration.ConfigurationSettings' does not contain a
definition for 'connecionStrings' C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\WebSites\WebSiteCompletionReport\Default.aspx
13 41 C:\...\WebSiteCompletionReport\
 
S

Steven Cheng [MSFT]

Hi Doug,

From your description, you encountered some problem when tried retrieving
connectionstrings from ASP.NET web.config programmtically, correct?

According to the code you provided, I think the problem is due to the
"ConfigurationSettings" class you used. In .NET framework 2.0 application,
you can use the following means to read connectionstring (in
<connectionStrings> section in app.config or web.config):

1. For ASP.NET application, you can use the
System.Web.Configuration.WebConfigurationManager class's
"ConnectionStrings" property to access connectionstrings defined in
web.config file, e.g.

=========================
using System.Web.Configuration;
......

protected void Page_Load(object sender, EventArgs e)
{
foreach (ConnectionStringSettings connstr in
WebConfigurationManager.ConnectionStrings)
{
Response.Write("<br/>" + connstr.Name + ": " +
connstr.ConnectionString);
}


}
==================

2. Or you can use "System.ConfigurationManager.ConnectionStrings" property,
this will work in not only ASP.NET Web application, but also other desktop
application(console or winform):

====================
protected void Page_Load(object sender, EventArgs e)
{
foreach (ConnectionStringSettings connstr in
ConfigurationManager.ConnectionStrings)
{
Response.Write("<br/>" + connstr.Name + ": " +
connstr.ConnectionString);
}
}
=========================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "dbuchanan" <[email protected]>
Subject: How to retreve connection string programmaticaly from this
Date: Tue, 15 Jul 2008 01:25:38 -0400
Here is part of the web.config - it is given to me like this.
=======================
<configuration>
<appSettings/>
<connectionStrings>
<add name="MyConnectionName" connectionString="Data
Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
=======================
How do I get the connection string?

The follwoing code is my attempt to connect

string cnStr;
cnStr = ConfigurationSettings. << At this point intellisense gives me the
following:

AppSettings
Equals
GetConfig
ReferencEquals

Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
code

cnStr =
ConfigurationSettings.connecionStrings["MyConnectionName"].ConnectionString ;

However I get the following error
Error 1 'System.Configuration.ConfigurationSettings' does not contain a
definition for 'connecionStrings' C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\WebSites\WebSiteCompletionReport\Default.aspx
13 41 C:\...\WebSiteCompletionReport\
 
S

Stan

Here is part of the web.config - it is given to me like this.
=======================
<configuration>
 <appSettings/>
 <connectionStrings>
  <add name="MyConnectionName" connectionString="Data
Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
 </connectionStrings>
 <system.web>
=======================
How do I get the connection string?

The follwoing code is my attempt to connect

string cnStr;
cnStr = ConfigurationSettings.  << At this point intellisense gives me the
following:

AppSettings
Equals
GetConfig
ReferencEquals

Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
code

cnStr =
ConfigurationSettings.connecionStrings["MyConnectionName"].ConnectionString­;

However I get the following error
Error 1 'System.Configuration.ConfigurationSettings' does not contain a
definition for 'connecionStrings' C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\WebSites\WebSiteCompletionReport\Default.aspx
13 41 C:\...\WebSiteCompletionReport\

Hi

Do this:

//put this namespace in scope

using System.Web.Configuration;


//then use the WebConfigurationManger to read the connection string

string ConStr =
WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

A bit confusing I know but you'll get used to it.

BTW use the same for any AppSettings in ASP.NET 2 thus:

string Astring = WebConfigurationManager.AppSettings["MyAppsetting"];

HTH
 
S

Steven Cheng [MSFT]

Hi Doug,

Does the information in previous message help you? Please feel free to post
here if there is anything else we can help on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Tue, 15 Jul 2008 06:40:10 GMT
Subject: RE: How to retreve connection string programmaticaly from this
 

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

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top