My learning curve has been almost straight up this weekend learning about encryption, encrypt for connection strings, access to app.config (primary config)and user.config, etc. So the question I have I will most likely will not use in my project, but I would like to know why I am getting an error. I have seen examples from jmc that seam to work fine.

When trying set values in the app.config file, I get the error “Object reference not set to an instance of an object.” Any help is appricated.


Code:
   Private Sub UpdateConfig(Optional ByVal OC As String = "", Optional ByVal Pass As String = "", _
   Optional ByVal UserN As String = "")
  
      'Open the primary config file.

        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Dim appSettings As KeyValueConfigurationCollection = config.AppSettings.Settings

        If Not OC = "" Then appSettings("OldConnectionString").Value = OC         <==  Error occurs here 
        If Not Pass = "" Then appSettings("Password").Value = Pass 
        If Not UserN = "" Then appSettings("UserName").Value = UserN 

        'Save the changes to the config file.
        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")

    End Sub