Solved

web.config connection string

Posted on 2006-07-16
6
966 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

718 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