Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with changing connection string that saved in app.config

Problem with changing connection string that saved in app.config

Scheduled Pinned Locked Moved C#
csharpvisual-studiocomhelptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hdv212
    wrote on last edited by
    #1

    Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks

    M C 2 Replies Last reply
    0
    • H hdv212

      Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks

      M Offline
      M Offline
      Md Marufuzzaman
      wrote on last edited by
      #2

      Could you share the code snippets…So that we can get the actual scenario properly?

      Thanks Md. Marufuzzaman


      Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

      H 1 Reply Last reply
      0
      • M Md Marufuzzaman

        Could you share the code snippets…So that we can get the actual scenario properly?

        Thanks Md. Marufuzzaman


        Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

        H Offline
        H Offline
        hdv212
        wrote on last edited by
        #3

        here is my Settings.Designer.cs code (my modification code is bold) :

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        //[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")]
        public string mehrConnectionString {
        get {
        return ((string)(this["mehrConnectionString"]));
        }
        set
        {
        this["mehrConnectionString"] = value;
        }

        }

            \[global::System.Configuration.**UserScopedSettingAttribute()**\]
            \[global::System.Diagnostics.DebuggerNonUserCodeAttribute()\]
            \[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)\]
            **//\[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")\]**
            public string mehrConnectionString1 {
                get {
                    return ((string)(this\["mehrConnectionString1"\]));
                }
                **set
                {
                    this\["mehrConnectionString1"\] = value;
                }**
            }
        

        here is my code to save in app.config :

        Properties.Settings.Default.serverName = this.txtServer.Text;
        Properties.Settings.Default.userName = this.txtUser.Text;
        Properties.Settings.Default.password = this.txtPass.Text;

                        if (this.radioLocal.Checked)
                        {
                            Properties.Settings.Default.network = false;
                            Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text);
                            Properties.Settings.Default.mehrConnectionString1 = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text);
                        }
                        else
                        {
                            Properties.Settings.Default.network = true;
                            Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;User Id={1};Password={2};", this.txtServer.Text, this.txtUser.Text, this.txtPass.Tex
        
        M 1 Reply Last reply
        0
        • H hdv212

          here is my Settings.Designer.cs code (my modification code is bold) :

          [global::System.Configuration.UserScopedSettingAttribute()]
          [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
          [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
          //[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")]
          public string mehrConnectionString {
          get {
          return ((string)(this["mehrConnectionString"]));
          }
          set
          {
          this["mehrConnectionString"] = value;
          }

          }

              \[global::System.Configuration.**UserScopedSettingAttribute()**\]
              \[global::System.Diagnostics.DebuggerNonUserCodeAttribute()\]
              \[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)\]
              **//\[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")\]**
              public string mehrConnectionString1 {
                  get {
                      return ((string)(this\["mehrConnectionString1"\]));
                  }
                  **set
                  {
                      this\["mehrConnectionString1"\] = value;
                  }**
              }
          

          here is my code to save in app.config :

          Properties.Settings.Default.serverName = this.txtServer.Text;
          Properties.Settings.Default.userName = this.txtUser.Text;
          Properties.Settings.Default.password = this.txtPass.Text;

                          if (this.radioLocal.Checked)
                          {
                              Properties.Settings.Default.network = false;
                              Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text);
                              Properties.Settings.Default.mehrConnectionString1 = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text);
                          }
                          else
                          {
                              Properties.Settings.Default.network = true;
                              Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;User Id={1};Password={2};", this.txtServer.Text, this.txtUser.Text, this.txtPass.Tex
          
          M Offline
          M Offline
          Md Marufuzzaman
          wrote on last edited by
          #4

          Probably you are loading the app.config several times, why dont you use a Hashtable for managing multiple connectionstring Example: Hashtable ObjHashtable   = new Hashtable(); ObjHashtable.Add("Key1", "Data Source= source_1 ;Initial Catalog= DatabaseName_1;User ID=id;Password=pwd;"); ObjHashtable.Add("Key2", "Data Source= source_2 ;Initial Catalog= DatabaseName_2;User ID=id;Password=pwd;"); You just need to load the Hashtable only one time...........    public SqlDataReader Example(string strSql, string strKey) {    ObjSqlConnection = new SqlConnection(ObjHashtable[strKey].ToString());    ...........    ....... }

          Thanks Md. Marufuzzaman


          Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

          H 1 Reply Last reply
          0
          • M Md Marufuzzaman

            Probably you are loading the app.config several times, why dont you use a Hashtable for managing multiple connectionstring Example: Hashtable ObjHashtable   = new Hashtable(); ObjHashtable.Add("Key1", "Data Source= source_1 ;Initial Catalog= DatabaseName_1;User ID=id;Password=pwd;"); ObjHashtable.Add("Key2", "Data Source= source_2 ;Initial Catalog= DatabaseName_2;User ID=id;Password=pwd;"); You just need to load the Hashtable only one time...........    public SqlDataReader Example(string strSql, string strKey) {    ObjSqlConnection = new SqlConnection(ObjHashtable[strKey].ToString());    ...........    ....... }

            Thanks Md. Marufuzzaman


            Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

            H Offline
            H Offline
            hdv212
            wrote on last edited by
            #5

            Thanks Md. Marufuzzaman i want to save my connection string in app.config to save this state in future use. next time it must use the saved settings which user set.

            M 1 Reply Last reply
            0
            • H hdv212

              Thanks Md. Marufuzzaman i want to save my connection string in app.config to save this state in future use. next time it must use the saved settings which user set.

              M Offline
              M Offline
              Md Marufuzzaman
              wrote on last edited by
              #6

              You are most welcome.......:) Absolutely you save your connectionString in your app.config. My point of view was: 1.     Read your config file 2.     Fill the HasTable 3.     Use the HasTable runtime so that you not need to invoke config file several times.

              Thanks Md. Marufuzzaman


              Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

              H 1 Reply Last reply
              0
              • M Md Marufuzzaman

                You are most welcome.......:) Absolutely you save your connectionString in your app.config. My point of view was: 1.     Read your config file 2.     Fill the HasTable 3.     Use the HasTable runtime so that you not need to invoke config file several times.

                Thanks Md. Marufuzzaman


                Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

                H Offline
                H Offline
                hdv212
                wrote on last edited by
                #7

                my problem is that i can't store my settings in app.config file. it's not persistence.

                1 Reply Last reply
                0
                • H hdv212

                  Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks

                  C Offline
                  C Offline
                  cjoki
                  wrote on last edited by
                  #8

                  I am looking into a simular issue and although, I am no expert, I think app.config is used in design time and [appname].exe.config is for release and where the changes need to be made... anyone, please correct me if I'm wrong on this. All the best cjoki

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups