locked
ConfigurationManager not reading ConnectionString RRS feed

  • Question

  • User-1107794116 posted

    Hi All,

     I'm encountering a very bizarre issue with a website I'm working on in VS2005 and v2.0. I have setup a single connection string in my web.config file and access it from several different classes. This is the connection string definition:  

        <connectionStrings>
    <add name="MyConnectionString" connectionString="Server=XXXXXX;Database=XXXXXX;uid=XXXXXX;Password=XXXXXX"/>
    </connectionStrings>
      I have a login page that pulls the connection string correctly with the following statement: 
    System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString 

    I have another page that uses this exact same statement, but fails to return an object. The error message is:

    "Object reference not set to an instance of an object"

     This happens because the "MyConnectionString" ConnectionString cannot be returned. Running in debug mode, I found that there was a connection string contained in the ConfigurationManager, however it had completely different data than what I have defined in my web.config. The name of this ConnectionString is "LocalSqlServer" and had a different value for the string. THIS IS NOT DEFINED ANYWHERE IN MY PROJECT! Where in the world is it coming from?

     

    To further compound my confusion - the other two developers I work with are running identical code on their machines and don't have this issue.

     

    Any ideas?

    Thanks, Dave

     

     

    Thursday, January 31, 2008 12:18 PM

Answers

  • User2064608025 posted

    if you go to iis and look at settings for asp.net for your application you can also see your config settings in there and to a certain extent edit them.

    You can also go to website -> Asp.Net Configuration to see how connectionstrings and security information is set up. You can also edit the settings from here too.

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2008 1:17 PM

All replies

  • User-322463337 posted

    So, your pages belong to the same application.... And the web.config file contains that only connection string (be careful on the name over here, I have seen that you have different names!!!)... It must be caused by some code that you have in the codebehind file!

    Good luck!

    Thursday, January 31, 2008 12:26 PM
  • User2064608025 posted

    it is added by default to your machine.config file when you install sql server express edition as part of your vs installation.

    Check out your machine.config for the entry:-

     <connectionStrings>
      <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
     </connectionStrings>

    Thursday, January 31, 2008 12:36 PM
  • User2064608025 posted

    Incidentally you can add the <clear/> tag in your connectionstrings section to ensure there are no connectionstrings to be taken into account other than those specified in your web.config.

    I usually place it before declaring any of my connectionstrings to ensure live servers machine.config files have no adverse effects such as those you are experiencing.

    Cheers

     

    Thursday, January 31, 2008 12:42 PM
  • User-1107794116 posted

    So, your pages belong to the same application.... And the web.config file contains that only connection string (be careful on the name over here, I have seen that you have different names!!!)... It must be caused by some code that you have in the codebehind file!

    Good luck!

    Thanks for pointing out the names! (I'm not sure how my company feels about me posting our class names so I tried to make up a fictional one, just forgot to change it everywhere!)

     

    Anyway, the names are correct in the project, but you are right in that I am using the information differently in each file. The question remains... why does it work on both of my co-workers' machines just fine, but doesn't work on mine? The code is identical (We've all deleted our repo's and re-loaded them) so I don't think it's an issue with the code itself.

    I'm really new to ASP.NET so I have little to no understanding of how the architecture works - at what point does the framework read in the data from web.config - and is there a way for me to tell during runtime where the web.config is being loaded from?

     

    Thanks 


     

    Thursday, January 31, 2008 1:05 PM
  • User2064608025 posted

    if you go to iis and look at settings for asp.net for your application you can also see your config settings in there and to a certain extent edit them.

    You can also go to website -> Asp.Net Configuration to see how connectionstrings and security information is set up. You can also edit the settings from here too.

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2008 1:17 PM
  • User-1107794116 posted

     Got it!!!

     It turns out that my problem was caused by the way my site was setup (a virtual directory off of the root) and the fact that I had links with a bad path. The links were incorrectly grabbing the application variable for the root, and as such, weren't inserting my virtual directory name at the beginning of the url. So, instead of trying to get to the page within my project, it was attempting to get it in directories off of the root. It wasn't there, but before it could determine that it executed some code that used the wrong web.config. I could see this when I opened IIS and looked at the configuration for this project (which showed the machine.config connectionstring as being crossed out due to the <clear/>) and the configuration for the root (which didn't have my specific connection string but DID have the LocalSqlServer).

     Thanks so much! That was an annoying 6 hour lesson learned today - but know I know in the future!!!
     

    Thursday, January 31, 2008 2:08 PM
  • User2064608025 posted

    If it is any consolation I went through a similar learning curve when using .Net 2.0. One thing to watch out for is if you ever use the security settings such as membership and so on. This can all be configured within the above post locations too.

    Not sure i solved it in 6 hours but somewhere in that region.

    All part of the fun of development eh?

    Glad I could help!

    Thursday, January 31, 2008 3:24 PM