Results 1 to 2 of 2

Thread: [2005] saving to app.config question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    [2005] saving to app.config question

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] saving to app.config question

    When you get a NullReferenceException the first thing to do is determine which reference is null. You can't fix something if you don't know what you're fixing. On that line there are only really two possibilities: appSettings or appSettings("OldConnectionString"). I'm not sure if the first one is possible but, if it is, it would be because your config file has no appSettings section. If appSettings is not Nothing then it must be that your appSettings section doesn't have a key named "OldConnectionString". That second is the more likely because, if you've done things the way I did in my example then you don't have a full connection string in the appSettings section. Full connection strings should be stored in the connectionStrings section.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width