Solved

Web.config <connectionStrings>

Posted on 2012-09-14
6
647 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
Comment Utility
PLease post how you are retrieving the connectionstring in your code
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
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
Comment Utility
Protected WithEvents sqlConn As SqlClient.SqlConnection
sqlConn = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Needed to add a reference to System.Configuration.dll.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio Code with .Net Core 5 15
SQl help with selection 14 43
Problem to go to Web page 2 46
Setting runtime form location 4 18
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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now