Solved

web.config connection string

Posted on 2006-07-16
6
963 Views
Last Modified: 2012-05-11
I want to verify if this is the right syntax in .net 2.0

<configuration>
      <appSettings/>
      <connectionStrings>
            <add name="MyConn" connectionString="Data Source=local;Initial Catalog=URLs; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes" providerName="System.Data.SqlClient"/>
      </connectionStrings>
      <system.web>

or should I  be doing it this way in .NET 2.0?

 <appSettings>
<add key="ConnectionInfo" value="server=(local);database=dbname;Integrated Security=SSPI" />
</appSettings>

In my code-behind I'm trying to access my connection like this (C#):

        GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]


When accessing this using a new instance of my class, what's the difference and which way should I be referencing my connection?

GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);
or
GetListURLs geturls = new GetListURLs(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);


I'm confused coming from VS 2003 to VS 2005
0
Comment
Question by:dba123
[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
6 Comments
 

Expert Comment

by:tfountain
ID: 17119544
By default VS 2005 will look to connectioStrings in your web.config for connections.  I use:
<connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="Employee" connectionString="Data Source=localhost;Initial Catalog=Employee;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

To retrieve:
System.Configuration.Configuration rootWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Employee");
            System.Configuration.ConnectionStringSettings connString = new ConnectionStringSettings();
            connString = rootWebConfig.ConnectionStrings.ConnectionStrings["Employee"];

I do not know that this is the best way but I took this right from Microsoft's help.

Hope this helps.
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17119584
0
 
LVL 1

Author Comment

by:dba123
ID: 17119589
I've seen that artible but it's talking about encryption and nowhere do I see an example of the correct connection string syntax...nor how to call it correctly in the way that I'm trying to do with my class constructor
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 2

Expert Comment

by:SKumar_1981
ID: 17119636
0
 
LVL 7

Accepted Solution

by:
irps20001 earned 500 total points
ID: 17121418
here is the another way to get the connection string.
string cnn = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;

Microsft is giving more info about ConnectionStrings, here is the link, may help you more
http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.connectionstrings.aspx
0
 
LVL 1

Author Comment

by:dba123
ID: 17123874
Finally,  I changed the connection string to use the correct SQL instance.  I didn't have it named as local in SQL 2005 Management Studio, I had it actually as pcname\LOCAL

that fixed it!
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.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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