how to change config.web file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dingjie
    New Member
    • Jul 2007
    • 7

    how to change config.web file

    <configuratio n>
    <appSettings/>
    <connectionStri ngs>
    <add name="pedbConne ctionString" connectionStrin g="Data Source=localhos t;Initial Catalog=acsdb;P ersist Security Info=True;User ID=sa;Password= password123"
    providerName="S ystem.Data.SqlC lient" />
    </connectionStrin gs>



    i want to change the initial catalog to lets say pedb? how do i do tat?




    Before:
    <add name="pedbConne ctionString" connectionStrin g="Data Source=localhos t;Initial Catalog=acsdb;P ersist Security Info=True;User ID=sa;Password= password123"

    After:
    <add name="pedbConne ctionString" connectionStrin g="Data Source=localhos t;Initial Catalog=pedb;Pe rsist Security Info=True;User ID=sa;Password= password123"


    thanks in advance
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    config.web should not be changed by it's own running code.
    Self-modifying code is naughty.
    What you could do is store it like this:
    Code:
    <add name="pedbConnectionString" connectionString="Data Source=localhost;Initial Catalog=#!CATALOG#;Persist Security Info=True;User ID=sa;Password=password123" />
    Or something and then when you grab the string, use a .Replace() to replace "#!CATALOG# " with the value you want.

    Comment

    • RoninZA
      New Member
      • Jul 2007
      • 78

      #3
      BUT, if you are trying to modify another application's code (like firing a seperate EXE to change the original config file), try using the System.Web.Conf iguration.WebCo nfigurationMana ger object for websites, or System.Configur ation.Configura tionManager object for windows apps (remember to include System.Configur ation reference).

      These objects' Open...() methods will give you a System.Configur ation.Configura tion object, which has all the configuration information parsed quite nicely :)

      I have a nifty class which I could paste here, but that would defeat the purpose, now wouldn't it?

      Happy hunting!

      Comment

      • dingjie
        New Member
        • Jul 2007
        • 7

        #4
        Originally posted by Plater
        config.web should not be changed by it's own running code.
        Self-modifying code is naughty.
        What you could do is store it like this:
        Code:
        <add name="pedbConnectionString" connectionString="Data Source=localhost;Initial Catalog=#!CATALOG#;Persist Security Info=True;User ID=sa;Password=password123" />
        Or something and then when you grab the string, use a .Replace() to replace "#!CATALOG# " with the value you want.
        how to replace the connection string in web.config?

        Comment

        • christopherpond
          New Member
          • Jul 2007
          • 26

          #5
          Not sure what you are trying to do but you can store multiple connection strings and access them by name in the web.config file.

          Comment

          • dingjie
            New Member
            • Jul 2007
            • 7

            #6
            Originally posted by RoninZA
            BUT, if you are trying to modify another application's code (like firing a seperate EXE to change the original config file), try using the System.Web.Conf iguration.WebCo nfigurationMana ger object for websites, or System.Configur ation.Configura tionManager object for windows apps (remember to include System.Configur ation reference).

            These objects' Open...() methods will give you a System.Configur ation.Configura tion object, which has all the configuration information parsed quite nicely :)

            I have a nifty class which I could paste here, but that would defeat the purpose, now wouldn't it?

            Happy hunting!
            that would be a taking out method. what abt an update method?

            Comment

            • dingjie
              New Member
              • Jul 2007
              • 7

              #7
              Originally posted by christopherpond
              Not sure what you are trying to do but you can store multiple connection strings and access them by name in the web.config file.

              can you please elaborate?

              i am trying to change the connection string.

              Comment

              • dip_developer
                Recognized Expert Contributor
                • Aug 2006
                • 648

                #8
                Originally posted by dingjie
                can you please elaborate?

                i am trying to change the connection string.

                hi dingjie......
                it's not a good idea to edit web.config at runtime.Realize that any change you make to web.config will result in the application being restarted on your webserver. This means all the sessions go bye-bye. The best way to store dynamic data like this, doesn't have to be in web.config, rather is in a database, or a xml config file you can parse at runtime, without interrupting the flow of things.

                but if you are using .net framework 2.0 then try this......

                Comment

                • christopherpond
                  New Member
                  • Jul 2007
                  • 26

                  #9
                  Why exactly are you trying to change it? Just curious, is it based upon some runtime data? Is it not known at design time? I usually keep 2 connection strings in my web.config. One for production and one for local development. I then pull them by name.

                  If your example is that you want to load a particular database based upon runtime parameters say UserID (if each user has its own database) this would not work.

                  Comment

                  • dingjie
                    New Member
                    • Jul 2007
                    • 7

                    #10
                    erm.. u see ar.. i got this system. when different schools login the database changes to cater to that school.

                    Comment

                    • christopherpond
                      New Member
                      • Jul 2007
                      • 26

                      #11
                      Not sure if this is a bad coding solution but how about create a method called GetConnectionSt ring which can access the connection string from the web.config and in that method could replace the catalog with the one you need. Just need to make sure all calls for connection string goes through this method.

                      All I can think of or build the whole connection string in code.

                      Comment

                      Working...