|
-
Oct 26th, 2008, 05:06 PM
#1
Thread Starter
Lively Member
[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
-
Oct 26th, 2008, 06:52 PM
#2
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|