Upadating web.config at runtime.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zacksoniar
    New Member
    • Sep 2007
    • 45

    #1

    Upadating web.config at runtime.

    I have following code in my application. It inserts a connection string in web.config at runtime as follows:

    Code:
    System.Configuration.Configuration objConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    System.Configuration.ConnectionStringSettings settingSqlServer = new System.Configuration.ConnectionStringSettings();
     settingSqlServer.Name="SQLSERVER";
                    settingSqlServer.ConnectionString=strConnectionStringSqlServer;
                    settingSqlServer.ProviderName="System.Data.SqlClient";
                    objConfiguration.ConnectionStrings.ConnectionStrings.Add(settingSqlServer);
    //saving changes in web.config
                    objConfiguration.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection(objConfiguration.ConnectionStrings.SectionInformation.Name);
    When I check web.config after executing above code, I am able to see connection string written in web.config. but when i try to access it, compiler throws error saying no such connection string exists. I am using EnterpriseLibra ry for database connectivity. The code to access connection string was:

    Code:
    objDB = DatabaseFactory.CreateDatabase("SQLSERVER");
    What am I doing wrong?
  • zacksoniar
    New Member
    • Sep 2007
    • 45

    #2
    The code is in Asp.net 4.0

    Comment

    • zacksoniar
      New Member
      • Sep 2007
      • 45

      #3
      Do I need to explain it in different way??

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Could you please post the code that is generated in your web.config for the connection?

        -Frinny

        Comment

        • zacksoniar
          New Member
          • Sep 2007
          • 45

          #5
          It will create connection string section like this:
          Code:
          <connectionStrings>
              <add name="SQLSERVER" connectionString="data source=EITL14\SQLEXPRESS;initial catalog=SearchDB_CASE;user id=sa;password=techdb;"
                providerName="System.Data.SqlClient" />
           </connectionStrings>

          Comment

          • zacksoniar
            New Member
            • Sep 2007
            • 45

            #6
            I found out that by adding following line to code , i will be able to reload config file:

            Code:
            Properties.Settings.Default.Reload();
            but I m not able to get this Properties object/class in my application.
            What namespace should i add?? my code is in c# - ASP.NET 4.0.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I'm not sure what to tell you.
              Everything you have looks correct....

              I remember a time when I had a problem updating a web application's files manually. I had installed the website (using an installer) and thought that it would be easier to just delete the existing files and replace them with the updated ones (rather than make a whole new installation package).

              What I found was that when I tried to delete the files Vista replaced them again...which meant that things didn't work properly because the old version didn't work with an updated component (that wasn't replaced)

              Maybe you're running into something like this....
              When you update the web.config your app domain is reset....maybe your OS is playing the same trick on you as it played on me.

              -Frinny

              Comment

              Working...