writing into config file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E481610
    New Member
    • Apr 2010
    • 13

    writing into config file

    how to write values into app config files using c# code in windows forms applications
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    config.AppSettings.Settings["newVal"].Value = "100";
    Regards
    Dheeraj Joshi

    Comment

    • E481610
      New Member
      • Apr 2010
      • 13

      #3
      about error in writing into the app.config file

      hi
      just now i have the the code
      config.AppSetti ngs.Settings["newVal"].Value = "100";
      for writing into app.config file
      but the error it is showing is
      "Object reference not set to an instance of an object."
      can u please clarify my doubt?

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        "Object reference not set to an instance of an object." means that you are referring to an object without a value. It can be solved by declaring the object as New. Can you post the code that gives you the error?

        Steven

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Does "newVal" exists in app.config?

          Regards
          Dheeraj Joshi

          To OP: Please do not start new thread when you have problem with same isuue. Continue posting in same thread. In this thread you asked how to write into app.config. Now you have problem in writing to it, so according to rules you need to post in same thread not to start new one.

          Comment

          • E481610
            New Member
            • Apr 2010
            • 13

            #6
            how to retrieve the written key and its value at once?

            hi i have written into the app config file by using the code
            config.AppSetti ngs.Settings.Ad d("srinu","100" );
            where "srinu" is the key , and "100" is its value
            now what i want to do is........ i have to display those key and its value in two different text fields by the click of the button
            can u please reply me fast?

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Get the value of the key from the app.config and display it.

              Something like this
              Code:
              String myVal = System.Configuration.ConfigurationSettings.AppSettings["newVal"].toString();
              Regards
              Dheeraj Joshi

              Comment

              • E481610
                New Member
                • Apr 2010
                • 13

                #8
                Originally posted by dheerajjoshim
                Get the value of the key from the app.config and display it.

                Something like this
                Code:
                String myVal = System.Configuration.ConfigurationSettings.AppSettings["newVal"].toString();
                Regards
                Dheeraj Joshi
                hi
                the code u sent is for the key and value that were in app.config file
                but what i want to retrieve is....to retrieve key and value that are externally written using some writing into app.config file codes
                just check the code below

                System.Configur ation.Configura tion config = ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None);
                /* XmlDocument doc =
                XmlNode node = doc.SelectSingl eNode("//appSettings");*/
                string c = config.AppSetti ngs.Settings["secondname "].Value = "100";
                //string k =Keys(config.Ap pSettings.Setti ngs["govinda"].Value) = "modifiedvalue" ;
                //config.AppSetti ngs.Settings.Ad d("Modification Date", DateTime.Now.To LongTimeString( ) + " ");
                //textBox2.Text = c;
                string[] keys = appSettings.All Keys;
                config.AppSetti ngs.Settings.Ad d("srinu","100" );
                config.Save(Con figurationSaveM ode.Modified);

                String myVal = System.Configur ation.Configura tionSettings.Ap pSettings["100"].ToString();
                textBox1.Text = keys[1];
                textBox2.Text = myVal;

                here "keys" is an array having only 2 values means upto keys[1] it exists
                but when i give textBox1.Text = keys[2];
                it is showing error eventhough i have written another "srinu" key into config file

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Originally posted by E481610
                  hi
                  the code u sent is for the key and value that were in app.config file
                  but what i want to retrieve is....to retrieve key and value that are externally written using some writing into app.config file codes
                  just check the code below

                  System.Configur ation.Configura tion config = ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None);
                  /* XmlDocument doc =
                  XmlNode node = doc.SelectSingl eNode("//appSettings");*/
                  string c = config.AppSetti ngs.Settings["secondname "].Value = "100";
                  //string k =Keys(config.Ap pSettings.Setti ngs["govinda"].Value) = "modifiedvalue" ;
                  //config.AppSetti ngs.Settings.Ad d("Modification Date", DateTime.Now.To LongTimeString( ) + " ");
                  //textBox2.Text = c;
                  string[] keys = appSettings.All Keys;
                  config.AppSetti ngs.Settings.Ad d("srinu","100" );
                  config.Save(Con figurationSaveM ode.Modified);

                  String myVal = System.Configur ation.Configura tionSettings.Ap pSettings["100"].ToString();
                  textBox1.Text = keys[1];
                  textBox2.Text = myVal;

                  here "keys" is an array having only 2 values means upto keys[1] it exists
                  but when i give textBox1.Text = keys[2];
                  it is showing error eventhough i have written another "srinu" key into config file
                  What is the error?

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Dheeraj Joshi
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1129

                    #10
                    Code:
                    String myVal = System.Configuration.ConfigurationSettings.AppSett ings["100"].ToString();
                    This is wrong it must be

                    Code:
                    String myVal=System.Configuration.ConfigurationSettings.AppSettings["secondname"].ToString();
                    Regards
                    Dheeraj Joshi

                    Comment

                    • E481610
                      New Member
                      • Apr 2010
                      • 13

                      #11
                      Originally posted by dheerajjoshim
                      What is the error?

                      Regards
                      Dheeraj Joshi
                      "object reference not set to an instance of an object"
                      this is the error

                      Comment

                      • MrMancunian
                        Recognized Expert Contributor
                        • Jul 2008
                        • 569

                        #12
                        @E481610: Stop creating new topics for this problem. Just post it all in the same topic.

                        Comment

                        • E481610
                          New Member
                          • Apr 2010
                          • 13

                          #13
                          Originally posted by E481610
                          hi i have written into the app config file by using the code
                          config.AppSetti ngs.Settings.Ad d("srinu","100" );
                          where "srinu" is the key , and "100" is its value
                          now what i want to do is........ i have to display those key and its value in two different text fields by the click of the button
                          can u please reply me fast?
                          hi dheeraj
                          can i get ur reply soon please?

                          Comment

                          • Dheeraj Joshi
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1129

                            #14
                            Keys is an array and array index starts from 0

                            Try keys[0] and keys[1]

                            Regards
                            Dheeraj Joshi

                            Comment

                            • tlhintoq
                              Recognized Expert Specialist
                              • Mar 2008
                              • 3532

                              #15
                              Since all of your threads are on the same subject of App.config they have all been merged.

                              In the future please keep one thread for one subject.

                              Comment

                              Working...