Solved

Web.config <connectionStrings>

Posted on 2012-09-14
6
665 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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