473,581 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to retreve connection string programmaticaly from this

Here is part of the web.config - it is given to me like this.
=============== ========
<configuratio n>
<appSettings/>
<connectionStri ngs>
<add name="MyConnect ionName" connectionStrin g="Data
Source=MyServer Name;Initial Catalog=MyDatab aseName;Integra ted Security=True"
providerName="S ystem.Data.SqlC lient"/>
</connectionStrin gs>
<system.web>
=============== ========
How do I get the connection string?

The follwoing code is my attempt to connect

string cnStr;
cnStr = ConfigurationSe ttings. << 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 =
ConfigurationSe ttings.connecio nStrings["MyConnectionNa me"].ConnectionStri ng;

However I get the following error
Error 1 'System.Configu ration.Configur ationSettings' does not contain a
definition for 'connecionStrin gs' C:\Documents and Settings\Admini strator\My
Documents\Visua l Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
13 41 C:\...\WebSiteC ompletionReport \
Jul 15 '08 #1
3 4657
Hi Doug,

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

According to the code you provided, I think the problem is due to the
"ConfigurationS ettings" class you used. In .NET framework 2.0 application,
you can use the following means to read connectionstrin g (in
<connectionStri ngssection in app.config or web.config):

1. For ASP.NET application, you can use the
System.Web.Conf iguration.WebCo nfigurationMana ger class's
"ConnectionStri ngs" property to access connectionstrin gs defined in
web.config file, e.g.

=============== ==========
using System.Web.Conf iguration;
......

protected void Page_Load(objec t sender, EventArgs e)
{
foreach (ConnectionStri ngSettings connstr in
WebConfiguratio nManager.Connec tionStrings)
{
Response.Write( "<br/>" + connstr.Name + ": " +
connstr.Connect ionString);
}
}
=============== ===

2. Or you can use "System.Configu rationManager.C onnectionString s" property,
this will work in not only ASP.NET Web application, but also other desktop
application(con sole or winform):

=============== =====
protected void Page_Load(objec t sender, EventArgs e)
{
foreach (ConnectionStri ngSettings connstr in
ConfigurationMa nager.Connectio nStrings)
{
Response.Write( "<br/>" + connstr.Name + ": " +
connstr.Connect ionString);
}
}
=============== ==========

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:
ms****@microsof t.com.

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "dbuchanan" <db*******@news group.nospam>
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/>
<connectionStri ngs>
<add name="MyConnect ionName" connectionStrin g="Data
Source=MyServe rName;Initial Catalog=MyDatab aseName;Integra ted
Security=True"
>providerName=" System.Data.Sql Client"/>
</connectionStrin gs>
<system.web>
============== =========
How do I get the connection string?

The follwoing code is my attempt to connect

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

AppSettings
Equals
GetConfig
ReferencEqua ls

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

cnStr =
ConfigurationS ettings.conneci onStrings["MyConnectionNa me"].ConnectionStri ng
;
>
However I get the following error
Error 1 'System.Configu ration.Configur ationSettings' does not contain a
definition for 'connecionStrin gs' C:\Documents and
Settings\Admini strator\My
>Documents\Visu al Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
13 41 C:\...\WebSiteC ompletionReport \
Jul 15 '08 #2
On 15 Jul, 06:25, "dbuchanan" <dbucha...@news group.nospamwro te:
Here is part of the web.config - it is given to me like this.
=============== ========
<configuratio n>
*<appSettings/>
*<connectionStr ings>
* <add name="MyConnect ionName" connectionStrin g="Data
Source=MyServer Name;Initial Catalog=MyDatab aseName;Integra ted Security=True"
providerName="S ystem.Data.SqlC lient"/>
*</connectionStrin gs>
*<system.web>
=============== ========
How do I get the connection string?

The follwoing code is my attempt to connect

string cnStr;
cnStr = ConfigurationSe ttings. *<< 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 =
ConfigurationSe ttings.connecio nStrings["MyConnectionNa me"].ConnectionStri ng*;

However I get the following error
Error 1 'System.Configu ration.Configur ationSettings' does not contain a
definition for 'connecionStrin gs' C:\Documents and Settings\Admini strator\My
Documents\Visua l Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
13 41 C:\...\WebSiteC ompletionReport \
Hi

Do this:

//put this namespace in scope

using System.Web.Conf iguration;
//then use the WebConfiguratio nManger to read the connection string

string ConStr =
WebConfiguratio nManager.Connec tionStrings["MyConnectionSt ring"].ConnectionStri ng;

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 = WebConfiguratio nManager.AppSet tings["MyAppsetti ng"];

HTH

Jul 15 '08 #3
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:
ms****@microsof t.com.

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

--------------------
>From: st*****@online. microsoft.com (Steven Cheng [MSFT])
Organization : Microsoft
Date: Tue, 15 Jul 2008 06:40:10 GMT
Subject: RE: How to retreve connection string programmaticaly from this
>
Hi Doug,

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

According to the code you provided, I think the problem is due to the
"Configuration Settings" class you used. In .NET framework 2.0
application,
>you can use the following means to read connectionstrin g (in
<connectionStr ingssection in app.config or web.config):

1. For ASP.NET application, you can use the
System.Web.Con figuration.WebC onfigurationMan ager class's
"ConnectionStr ings" property to access connectionstrin gs defined in
web.config file, e.g.

============== ===========
using System.Web.Conf iguration;
.....

protected void Page_Load(objec t sender, EventArgs e)
{
foreach (ConnectionStri ngSettings connstr in
WebConfigurati onManager.Conne ctionStrings)
{
Response.Write( "<br/>" + connstr.Name + ": " +
connstr.Connec tionString);
}
}
============== ====

2. Or you can use "System.Configu rationManager.C onnectionString s"
property,
>this will work in not only ASP.NET Web application, but also other desktop
application(co nsole or winform):

============== ======
protected void Page_Load(objec t sender, EventArgs e)
{
Jul 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3295
by: Dmitry Karneyev | last post by:
Hi All! I've got an ado.net app with several win forms. In the first form (form1) I connect to DB and in other forms I use connection string generated by the form1 and stored in sqlconnection object of form1. If I use windows authorization of SQL Server everything works fine, but when I use SQL Server authorization it doesn't work because...
4
2755
by: Mark | last post by:
OK. Here we go. I have an ASP.NET application that does many hits to a SQL Server DB on a separate server. When I first created this application (2 years ago) and was very new to ASP/ASP.NET, to make the SQL connection string global throughout the application I created a .vb file within the application project and declared a public...
3
7760
by: steve | last post by:
Hi, I heard you could put your db connection string into the web.config file to make it editable after you compile and/or deploy. I cant seem to find any info on this.. can any one help? Sample code would be nice Cheers Steve
3
1615
by: rc | last post by:
Hello, We have several asp.net pages (.aspx) that access SQL server. Currently, we use a connection string like this: Conn=New SqlConnection("Server=servername;uid=id;pwd=password;database=db")
5
1666
by: Fernando Lopes | last post by:
Hi there. In your opinion, where is the best place to put the connection string of a web application? Web.config, a constant into the code? I'm not using a component server, so it's not an option, ok? Tks. Fernando
2
3258
by: Orit | last post by:
Hello . Please find below my questions - I hope some of the ASP.NET experts of this forum will answer to those beginner's questions : My Web site should be able to work with either SQL Server or Access database as following : It will connect first to the Master database (SQL or Access, connection String of this database should be read...
1
1877
by: Orit | last post by:
Hello . I am stucked with the following problem and hope you can help me : - My ASP.NET 2.0 application has one master database . - This master database has a Databases table with list of children databases . - All children databases have the same schema ( only the data is different ) . My application connects to the Master database...
1
1998
by: sujatha k | last post by:
?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="enterpriselibrary.configurationSettings" type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <enterpriselibrary.configurationSettings...
0
1148
by: sujatha k | last post by:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections <sectionname="enterpriselibrary.configurationSettings" type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <enterpriselibrary.configurationSettings...
0
7882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.