1

I have a project by someone else and there is this line of code:

ConnectionStringSettings myConnectionStringSetting = ConfigurationManager.ConnectionStrings[0];

which for some reason returns this:

ConfigurationManager.ConnectionStrings[0] is returning {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}                System.Configuration.ConnectionStringSettings

I don't have selexpress installed to my knowledge and my web.config points to my server using a different connection string. Where do I find the list of all the connection strings in VS 2010?

Doing an find on the entire solution returns no results for sql express either.

2 Answers 2

4

Just try to insert an <clear/> tag before your ConnectionsString.

 <connectionStrings>
  <clear/>
  <!--You connectionString go here-->
 </connectionStrings>
Sign up to request clarification or add additional context in comments.

2 Comments

Is that what clear does? It cleans all the connection strings and makes connectionstring["0"] be the one right after clear?
@chris The clear tag clean the default connectionString which is : source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
0

About Connection Strings and Configuration Files

About Connection Strings & the Web.config File

Example from MSDN:

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
            if (connString != null)
                Console.WriteLine("Northwind connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No Northwind connection string");
        }

3 Comments

Sorry, but you should know that a link only answer is not considered a good answer also if the link contains the answer for the OP. Links die and your answer becomes useless to future readers. Try to elaborate or just write a comment.
The problem is I have a SQLEXPRESS connection string when I debug but it appears no where in the solution or in the web.config. The web.config is the correct one.
It's much pretty to use name as key to access it: ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[name]; Also you may look this question link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.