?
Solved

Why am I getting xmlNode,xmlElement, and AppSettings is not defined?

Posted on 2007-09-27
17
Medium Priority
?
556 Views
Last Modified: 2013-11-07
I found this VB.Net code on another question and tried to implement it in my program.  I have narrowed down to 3 errors that have me baffled.  I am using VS 2003 in an XP Pro environment.  

errors:
Performing main compilation...
...: error BC30002: Type 'AppSettings' is not defined.
... : error BC30002: Type 'XmlNode' is not defined.
... : error BC30002: Type 'XmlElement' is not defined.
Building satellite assemblies...      

Code:
 Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String = ""
        s = Me.getConfigValue("v4")
        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As XMLDocument = New XMLDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
' *********Here is the first problem

            Dim appSettingsElement As _
              XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
'***********
            ' create a new <add> element

' *********Here is the second problem

            Dim newElement As XmlElement = _
               Xml.createElement("add")
'*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
    End Sub
0
Comment
Question by:mtsnh
  • 10
  • 7
17 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 19976560
I am not sure about these errors but if you are looking to add app setting keys programatically. here is a way to do it
http://msmvps.com/blogs/simpleman/archive/2005/06/23/54733.aspx

0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19976570
Also it may have occurred because of the escape characters. try "//configuration//appSettings"
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19976592
I used this code and it worked fine...

Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String
        's = Me.getConfigValue("v4")

        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As Xml.XmlDocument = New Xml.XmlDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
            ' *********Here is the first problem

            Dim appSettingsElement As _
              Xml.XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
            '***********
            ' create a new <add> element

            ' *********Here is the second problem

            Dim newElement As Xml.XmlElement = _
               Xml.createElement("add")
            '*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            'Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:mtsnh
ID: 19977886
Thanks for your help.  I believe we are on to something.  When I place this in my code the problem seems to go away, however depending on where I put it, it causes more problems.  

Public Interface ConfigurationManager
    Inherits IDispatch
What is this doing and where should it be placed.  I found it in here:

http://msmvps.com/blogs/simpleman/archive/2005/06/23/54733.aspx
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19978193
Well isnt this link as the link i made available in my first post. Confused :)

What problems are you facing doing this? Because i was able to successfully able to add the key into my config file dynamically
0
 

Author Comment

by:mtsnh
ID: 19979285
It is like a reference is missing or the interface and inherits from these lines.

Public Interface ConfigurationManager
    Inherits IDispatch

If I add these to lines to the top of my code, my original problem goes away however I now get many more errors with the rest of my code and I think this is the cause:  

.... Interface members must be methods, properties, events, or type definitions.

I have never done anything with interface members so I am slightly confused to what is going on.

Thanks again,

Kevin
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19982082
I think the link talks about how to add the appsetting keys using .net 2.0, since you are using .net 1.1 it might not work. I didnt factor the .net 1.1 limitation because u mentionned that u are using VS2003 but again since it was posted in .net framework 2.0 i posted this link.

The code that i had pasted works fine. I have tested it and it ended up adding the app setting key. I paste the code again here. Please use this and tweek it, it should work just fine.

Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String
        's = Me.getConfigValue("v4")

        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As Xml.XmlDocument = New Xml.XmlDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
            ' *********Here is the first problem

            Dim appSettingsElement As _
              Xml.XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
            '***********
            ' create a new <add> element

            ' *********Here is the second problem

            Dim newElement As Xml.XmlElement = _
               Xml.createElement("add")
            '*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            'Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
0
 

Author Comment

by:mtsnh
ID: 19982362
I finally figured it out!!!!!  Yippee.   I was missing one stupid line of code!

Imports System.Xml

Thanks for your help.
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19982780
argghhhh !!!! I was looking at the code assuming the imports were already in place...
0
 

Author Comment

by:mtsnh
ID: 19982824
There is still something that I am not doing correct.  That code works fine but I don't think it is doing what I planned.  It seems to be overwriting the config file everytime and I can't figure out how to read a value.  There are to many assumptions made in all the examples I have studied.  I still have something missing in order to read a value in a config file.  When I try the code below, I still get type appsettings is not defined which is either a missing reference or import.  Any new ideas would be helpful.

        Dim a As New AppSettings(AppSettings.Config.PrivateFile)
        Dim myString As String = a.GetSetting("SettingName")
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19982902
Try this
Dim myString as String = ConfigurationSettings.AppSettings("SettingName")
0
 

Author Comment

by:mtsnh
ID: 19983931
Thanks!  That got rid of the error but I am still not getting a value.  Is that file being replaced everytime it is being run?  What is causing that to happen if it is?  We are very close to solving this mystery!
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19983994
There will be two app config files. One in the project called app.config and another one by the name Applicationname.exe.config

Which one has the changed value after u run your program??
0
 

Author Comment

by:mtsnh
ID: 19984058
I was under the impression that this code only deals with the Applicationname.exe.config file.  Haven't really paid any attention to the other one.  That appears to have been the same for a very long time.
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 800 total points
ID: 19984076
So everytime u compile ur program, the app.config which doesnt have the app setting that u were adding by code gets copied as app.exe.config. so everytime it ends up adding the key.

try running this not from VS but by directly executing the exe from the debug folder
0
 

Author Comment

by:mtsnh
ID: 19984093
Thank you ! Thank you ! Thank you !  I was wondering where all the information that kept getting written to the Applicationname.exe.config file was coming from.  I didn't see it anywhere in the code and it was a mystery until now.  I never really got a good explaination of the app.config file.  Now I know that is where I want to make my changes that I want to distribute.  It is all coming together for me now.

Thanks very much!
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 19984161
you are welcome. Best of luck :)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question