Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2010, 08:04 AM
Registered User
 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question App.Config and Web.Config conflict

I am currently developing a destop application which retrieves data from web service.

I have both projects in my solution.

I have a web.config in the Web Service project which details a connection string. I also have an app.config in the other project which also details a connection string.

When I try to access the connection string from the Web Service project using the ConfigurationManager class my project picks up the connection string from the app.config.

Why is this happening and how do I set it so it picks up from the web.config in the same project?

(A portion from app.config in my desktop project)
<connectionStrings>
<add name="DSWCLink.My.MySettings.DSWCConnectionString" connectionString="Data Source=2003SQL;Initial Catalog=databasename;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>




(A Portion from my web.config in my Web Service project)

<connectionStrings>

<add name="DSWCConnectionString"
connectionString="Data Source=2003SQL;Initial Catalog=DBLink;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>


(The code in my web service project)
Dim strConnectionString As String = ConfigurationManager.ConnectionStrings(0).Connecti onString

Dim sqlConn As New SqlConnection(strConnectionString)

(The strConnectionString picks up the value from app.config in a different project. The desktop app)

Hope this makes sense.
 
Old March 9th, 2010, 11:18 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You can choose the string by name instead of index.

Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("DSWCConnec tionString").Connecti onString
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 9th, 2010, 11:28 AM
Registered User
 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red face Error Message

I used the index for quickness. If I specify the name I get the following error:

"GetData Object reference not set to an instance of an object."

This is based on the following line of code:

Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("DSWCConnec tionString").ConnectionString

The reason for this is that the ("DSWCConnectionString") exists in web.config but not app.config (which is correct as the web service is only added to the desktop project for debugging)

If I specify 0 it picks up the connection string from app.config.

More Info. (Just incase I am not making myself clear)

I am developing a desktop app that sends and receives data to a web service which I am also developing.

I have one solution which includes the Web Service project and the Desktop project. I have added a reference to the web service project to my desktop project.

The code I am having trouble with is the GetData function in the Web Service Project. (This is where I use the ConfirgurationManager.Conn... code)

Instead of picking up from Web.Config in that project, the connection string returned is from the App.Config file in my desktop project.

Hope this is more clear.
 
Old March 9th, 2010, 11:35 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Hmmm... I see.

Have you tried WebConfigurationManager instead of ConfigurationManager? Mostly the same API. As I understand it, main difference is that WebConfigurationManager looks in web.config files only.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 9th, 2010, 11:49 AM
Registered User
 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Still the same

I have tried using the following code:

Dim strConnectionString As String = System.Web.Configuration.WebConfigurationManager.C onnectionStrings("DSWCConnectionString").Connectio nString


But still get the error:

"GetData Object reference not set to an instance of an object."
 
Old March 9th, 2010, 11:51 AM
Registered User
 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also when using the WebConfigurationManager code and specifying an index of 1 instead of the name returns the connection string from app.config.





Similar Threads
Thread Thread Starter Forum Replies Last Post
web.config vs. app.config darlo Visual Studio 2005 11 August 20th, 2008 07:23 AM
How to get the directory of app.config cczhangjm C# 2 May 28th, 2007 02:55 AM
configure web.config (db details) when deploy app? benilpj ASP.NET 2.0 Basics 3 June 7th, 2006 09:27 PM
Chapter 8 - app.config jotto BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 2 March 21st, 2006 05:31 PM
Best way to bind config data to a web app psantos Pro JSP 2 October 21st, 2003 06:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.