Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

 

 

 

I want to save in my WPF application data in the settings.

The settings should appear at runtime.

However, an error message comes when the data is finally stored in the settings.

 

The following C # code is usually recommended to save settings.

Properties.Settings.Default["Test_String_Setting"] = "Hallo";

 

error message

System.NullReferenceException occurred

  HResult=0x80004003

  Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

  Source=<Cannot evaluate the exception source>

 

 

 

Solution: you can write the value into the DefaultValue.

Then you must also ask this again.

Properties.Settings.Default.Properties["Test_String_Setting"].DefaultValue  = "Hallo";

 

 

 

Complete C # code in WPF

A settings value is added at runtime and added to the application settings

private void save_Settings()

        {

            //----------< save_Settings() >----------

            //System.Configuration

 

            //< Setting erstellen >

           

            SettingsProperty prop = new System.Configuration.SettingsProperty("Test_String_Setting");

            prop.PropertyType = typeof(string);           

            Properties.Settings.Default.Properties.Add(prop);

           

            Properties.Settings.Default.Save();

//*ok

            Properties.Settings.Default.Properties["Test_String_Setting"].DefaultValue  = "Hallo";

 

//*fehler

            //Properties.Settings.Default["Test_String_Setting"] = "Hallo";

            //</ Setting erstellen >

 

            //< Settings speichern >

            Properties.Settings.Default.Save();

            //</ Settings speichern >

 

 

           

 

            //----------</ save_Settings() >----------

        }

 

        private void read_Settings()

        {

            //----------< save_Settings() >----------

            //System.Configuration

 

            //< Setting erstellen >

            if (Properties.Settings.Default.Properties["Test_String_Setting"] != null)

            {

                string sTest = Properties.Settings.Default.Properties["Test_String_Setting"].DefaultValue.ToString() ;

                MessageBox.Show(sTest.ToString());

            }

            //</ Setting erstellen >

 

            //< Settings speichern >

            Properties.Settings.Default.Save();

            //</ Settings speichern >

 

            //----------</ save_Settings() >----------

        }

 

Mobile
»
WPF: Create, write and read settings dynamically
»
WPF: Create settings at runtime

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de