Solved

web.config connection string

Posted on 2006-07-16
6
926 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

16 Experts available now in Live!

Get 1:1 Help Now