Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Connection String in web config file

I am trying to create a connection string to store in my web config file.  I want to later call that string and execute a command with it.  What is the correct syntax for doing this?  I'm new to web apps and c# in general so any help is appreciated.

Here is my string:
sqlConnectionString="Data Source=ServerName;initial catalog=myDatabase;persist security info=False;user id=UserName;Password=Password"
0
PsychoDazey
Asked:
PsychoDazey
  • 4
  • 3
1 Solution
 
tusharashahCommented:
Add your ConnectionString inside appSetting in Web.Config like following:

<configuration>
   <appSettings>
     <add key="connectionString"  value="Data Source=ServerName;initial catalog=myDatabase;persist security info=False;user id=UserName;Password=Password"/>
    </appSettings>
   
  <system.web>
              ...
              ...
   </system.web>
</configuration>


Now in your code to access this value:

using System.Configuration;

string strConnectionString = ConfigurationSettings.AppSettings("ConnectionString");


-tushar
0
 
PsychoDazeyAuthor Commented:
That fixed it...I was trying to put it before <configuration>

Thanks!
0
 
ayha1999Commented:
Hi,

try this

<configuration>
   <appSettings>
     <add key="connectionString"  value="Data Source=ServerName;initial catalog=myDatabase;persist security info=False;user id=UserName;Password=Password"/>
    </appSettings>
   
  <system.web>
              ...
              ...
   </system.web>
</configuration>

Now in your code to access this value:

use--> System.Configuration

SqlConnection Con = new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));

hope this helps.

ayha
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PsychoDazeyAuthor Commented:
Oops, spoke too soon.  It doesn't like the ConfigurationSettings.AppSettings line:

c:\inetpub\wwwroot\CarrierDashboard\NewUser.aspx.cs(127): 'System.Configuration.ConfigurationSettings.AppSettings' denotes a 'property' where a 'method' was expected
0
 
tusharashahCommented:
yeah.. change () to []...

string strConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

-tushar
0
 
PsychoDazeyAuthor Commented:
Thanks tushar.  I miss vb 6.0.......
0
 
tusharashahCommented:
Don't worry.. you'll have fun soon :)
0
 
PsychoDazeyAuthor Commented:
I hope so, thats what they keep telling me!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now