WebConfigurationManager.ConnectionStrings in aspx.cs versus ConnectionString in web.config RRS feed

  • Question

  • I run a website on an ISP where my community college students can practice their Ancient Greek and Latin.  Most of the aspx pages I designed myself, but I had to hire someone to write the database connections, as I am weak in this area.  Now I’ve downloaded all the pages from the ISP in order to build a duplicate site on my home network.  I’ve also installed the database that runs on the ISP on the same home machine.  Here’s my problem.

    On the pages where the connection string is on the aspx page it was easy enough to simply change the connection string on the web.config page to point to the local SQLexpress:


    <add name="HomeDB" connectionString="Data Source=HomeMachine\SQLEXPRESS;Initial Catalog=HomeDB;Integrated Security=True"

    providerName="System.Data.SqlClient" />


     Those pages I got to work.  On other pages, however, my contractor used WebConfigurationManager. ConnectionStrings[“LocalSqlServer”].ConnectionString); in the aspx.cs page.  These I cannot get to connect to the local SQLexpress. Obviously, I’m not connecting the dots here.  Something besides LocalSqlServer needs to be changed.  But what?

    I’d also like to know when and why I should connect to the database using WebConfigurationManager in the aspx.cs page rather than a connectionString on the web.config page.

    Thanks for any help you can give me.  My students find the language drills quite helpful, and I would like to improve the site even more.  I just wish I knew more web programming.

    Friday, May 10, 2013 8:53 PM

All replies

  • Hi, WebConfigurationManager. ConnectionStrings is essentially just a lookup to translate to the correct connection string (as you are using on your web.config page).  Does your web.config page have a connectionString setting with a value for "LocalSqlServer"? 

    You may be able to change the WebConfigurationManager.ConnectionString to use your "HomeDB" connection definition. 

    WebConfigurationManager. ConnectionStrings[“HomeDB”].ConnectionString

    Sam Lester (MSFT)


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    • Proposed as answer by Fanny Liu Monday, May 13, 2013 7:47 AM
    • Marked as answer by Fanny Liu Wednesday, May 15, 2013 7:44 AM
    • Unmarked as answer by Oliphant Smeaton Wednesday, May 15, 2013 5:46 PM
    • Unproposed as answer by Oliphant Smeaton Wednesday, May 15, 2013 5:46 PM
    Friday, May 10, 2013 9:12 PM
  • Sorry to be so late in getting back. (Finals week.) 

    No, my web.config page just has the HomeDB connectionString setting I posted.  However, I think the problem may lie elsewhere.  I’m getting the infamous “type or namespace could not be found (are you missing a using directive or an assembly reference?” This then point to the following entry in the aspx.cs file: private DBContextDataContext dbContext = new DBContextDataContext(WebConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString);

    Needless to say, I haven’t a clue why it can’t find the namespace that it obviously finds at the ISP.

    Thanks again for any help you can give me.  It’s really frustrating when you know the solution is just a word or two away.

    Wednesday, May 15, 2013 5:47 PM