0

I am trying to add new list item to settings.settings file. I have modified my settings.settings file as below

enter image description here

and on button click i have the following code:

 private void button7_Click(object sender, EventArgs e)
        {
string tempConString = "server='.\sqlexpress';database='somedatabasename';Trusted_Connection=Yes;";
 Properties.Settings.Default.connectionString.Add(tempConString);
                                Properties.Settings.Default.Save();
}

i am getting NullReferenceException with message Object reference not set to an instance of an object.

I want to save multiple strings to Properties.Settings.Default.connectionString

Tan
  • 778
  • 3
  • 18
  • 36
  • look ...the value is empty! – M.kazem Akhgary May 10 '16 at 14:43
  • Duplicate? http://stackoverflow.com/questions/2890271/how-to-save-a-liststring-on-settings-default – 3615 May 10 '16 at 14:44
  • sorry, i have read the post which you have given. by reading that post only i have modified my code, but i am getting nullreference error. how to overcome this is my question. – Tan May 10 '16 at 14:45
  • How did you get `System.Collections.Generic.List` to show up in your settings? Even when I click "browse" I don't see a `System.Collections`. – Quantic May 10 '16 at 14:52
  • i followed the instructions from http://stackoverflow.com/questions/2890271/how-to-save-a-liststring-on-settings-default – Tan May 10 '16 at 14:53

1 Answers1

1

To achieve desired behavior you should follow the Accepted Answer of the post. I don't understand why a wrong answer is so upvoted.

Never the less, just pick System.Collection.Specialized.StringCollection from Type DropDown and insert your values from new lines. And where you want to use it do something like this:

Console.WriteLine(Settings.Default.test.Cast<string>().ToList().FirstOrDefault());

Everything was in that post, to be honest.

Follow instructions from http://csharphelper.com/blog/2011/08/use-a-setting-that-contains-a-string-collection-in-c/

Community
  • 1
  • 1
3615
  • 3,787
  • 3
  • 20
  • 35
  • can you please help me how to add a new string from code behind, when i tried properties.settings.default.constring.add("somestring") giving me error – Tan May 10 '16 at 17:49
  • What kind of error? What does the error say? Code behind of the aspx page? Code behind of WPF view? Please, provide more information. – 3615 May 11 '16 at 06:09
  • i am using in winforms application. ".cs" file, http://csharphelper.com/blog/2011/08/use-a-setting-that-contains-a-string-collection-in-c/ this is what exactly i am trying to do but i am still getting error "Value cannot be null. Parameter name: source" – Tan May 11 '16 at 06:18
  • Ok, I was testing it with WPF app. But basically in winforms it's the same. Check this post: http://stackoverflow.com/a/6611972/5246145. Currently I've tested it in WinForms application and the steps are the same as described, the only difference, manually introduce something from designer, so the Collection would be initialized. – 3615 May 11 '16 at 06:27
  • thanks it worked now, the mistake i was doing is i have initially set "connectionString" to type "string" and later modified the type to "System.Collections.Specialized.StringCollection" so it was giving error after that i have created a new name "conString" and specified type as "System.Collections.Specialized.StringCollection" and updated my code and followed instructions from the link http://csharphelper.com/blog/2011/08/use-a-setting-that-contains-a-string-collection-in-c/ now its working. – Tan May 11 '16 at 09:50