Solved

Web.config <connectionStrings>

Posted on 2012-09-14
6
671 Views
Last Modified: 2012-09-14
I'm trying to figure out the connection string for Web configs. I have:
Web.Debug.config:
<connectionStrings>
    <add name="ConnectionString"
      connectionString="data source=MY_DEV_DATABASE;persist security info=True;initial catalog=MyCatalog;user id=DevUser;password=123456"
      xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>

Open in new window

Web.Release.Config:
<connectionStrings>
    <add name="ConnectionString"
      connectionString="data source=MY_DATABASE;persist security info=True;initial catalog=MyCatalog;user id=ProductionUser;password=123456"
      xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>

Open in new window

Web.config:
<connectionStrings>
    <add name="ConnectionString"
      connectionString="data source=MY_DEV_DATABASE;persist security info=True;initial catalog=MyCatalog;user id=DevUser;password=123456"/>
</connectionStrings>

Open in new window


First, I'm just trying to get this to work in Visual Studio (not publlishing yet), so I'm just concerned about Web.config. As it is, it doesn't work, saying The ConnectionString property has not been initialized.. It works if I do:
  <appSettings>
    <add key="ConnectionString" value="data source=EMNRD-DEV-SQL08;persist security info=True;initial catalog=MMDCoal;user id=CoalMinesQueryUser;password=CoalMinesQueryUser"/>

Open in new window

but I'm trying to user <connectionStrings> instead of <appSettings>

What am I doing wrong?
0
Comment
Question by:allelopath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38399064
PLease post how you are retrieving the connectionstring in your code
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38399095
Connection string in web.config :

<connectionStrings>
    <add name="ConnectionString" providerName="System.Data.SqlClient"
      connectionString="data source=MY_DEV_DATABASE;persist security info=True;initial catalog=MyCatalog;user id=DevUser;password=123456"/>
</connectionStrings>

Open in new window


retrieve in code
using System.Configuration;
...
...

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 38399103
Protected WithEvents sqlConn As SqlClient.SqlConnection
sqlConn = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Open in new window

0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 38399136
Change to :

Protected WithEvents sqlConn As SqlClient.SqlConnection
sqlConn = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

Open in new window


Also need to import the System.Configuration namespace
0
 
LVL 1

Author Comment

by:allelopath
ID: 38399196
I added this at the top of the file:
Imports System.Configuration

but I get this error on the line you suggested to add (which is undoubtedly what I need)
ConfigurationManager is not declared. It may be inaccessible dues to its protection level.

If I type a . after System.Configuration, ConfigurationManager is not an available selection.
0
 
LVL 1

Author Comment

by:allelopath
ID: 38399279
Needed to add a reference to System.Configuration.dll.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question