Solved

Web.config <connectionStrings>

Posted on 2012-09-14
6
660 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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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