Reading a <sectionGroup> in app.config

  • Thread starter Thread starter kevininstructor
  • Start date Start date
K

kevininstructor

I have been trying to get the code at the page below to work in .NET 1.1 but
can not seem to get the example to work in VB.NET, any ideas what might be
the problem?

<a
href="http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondeclaringsectiongroups.asp?frame=true">Declaring
and Accessing Section Groups (.NET Framework Developer's Guide)</a>

Note: I posted under microsoft.public.dotnet.language.vb with no luck so I
am now trying this forum.

Thanks for any thoughts,
Kevin
 
There is a VB.Net example in the article. Is that what you say youy can not
see to get to work? If so, what exactly is the problem?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Yes, I am working on the VB.NET example. Here is the error reported back
from the code:

Error:
An unhandled exception of type 'System.Configuration.ConfigurationException'
occurred in system.dll
Additional information: Could not create
System.Configuration.NameValueSectionHandler,System
 
Are you declaring the custom section in the <configSections>? I find that is
often an easy one to forget.
 
I am using the exact code without any modifications from the example in the
webpage from Microsoft below. Figured if the example worked then the next
step would be to modify it for my use but have only copied and pasted the
code so far.
 
Well, at this point I can't say without seeing the code you're working from.
Can you post it?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Okay, as mentioned before, this code is unaltered code copied and pasted
from
http://msdn.microsoft.com/library/d.../cpguide/html/cpcondeclaringsectiongroups.asp
and placed into a) app.config b) a button Click event to trigger the code.
My logic was to not alter any of the MSDN code until it worked. Also, I
placed this code presented below from MSDN into a new Windows forms project
so there is nothing to conflict with the code from MSDN. Please note my
problem is with the read from <sectionGroup name="mySectionGroup">.

Thanks for your time on this matter


<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
<!--The following code declares a section group called
mySectionGroup. -->
<sectionGroup name="mySectionGroup">
<section name="mySection"
type="System.Configuration.NameValueSectionHandler,System" />
</sectionGroup>
</configSections>

<mySectionGroup>
<mySection>
<add key="key1" value="value1" />
</mySection>
</mySectionGroup>

</configuration>Dim nvc As NameValueCollection

' Notice the path-like syntax.
nvc = CType(ConfigurationSettings.GetConfig("mySectionGroup/mySection"),
NameValueCollection)
 
Are you referencing the System.Configuration dll? Just another off the top
of my head thought.
 
Yes, here are my imports

Imports System.Configuration
Imports System.Collections.Specialized
 
What type of project is it (console, winforms, web, library) and what is the
name of the configuration file?
 
Okay, while working with the various people assisting me I decided since the
error message as shown below indicates a problem with the "type" of section
"mySection" it seems logical now that the second argument might be the
issue. I thought, let's see what happens when I take the second arg out
which is "system", rebuild and see what happens. It ran without error. So I
added the following code
....
If nvc.Count > 0 Then
MessageBox.Show(nvc("Key1"))
End If

NOW the question is, why did the second argument make the example blow up?
Is the second argument needed? If the second argument is truly needed then
it must not be "system" but then what is the second argument suppose to be?



<BEGIN ORIGINAL ERROR MESSAGE>
An unhandled exception of type 'System.Configuration.ConfigurationException'
occurred in system.dll
Additional information: Could not create
System.Configuration.NameValueSectionHandler,System
</END ORIGINAL ERROR MESSAGE>


Here is what I have now:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
<!--The following code declares a section group called
mySectionGroup. -->
<sectionGroup name="mySectionGroup">
<section name="mySection"
type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>

<mySectionGroup>
<mySection>
<add key="key1" value="value1" />
<add key="Connection" value="Value of Connection" />
</mySection>
</mySectionGroup>
</configuration>
....
Imports System.Configuration
Imports System.Collections.Specialized
....
Dim nvc As NameValueCollection
nvc = CType(ConfigurationSettings.GetConfig("mySectionGroup/mySection"),
NameValueCollection)
If nvc.Count > 0 Then
MessageBox.Show(nvc("Connection"))
End If
 
Mike,

I have successfully ran the code now and have replied back to my own
question with what I did to get the code to execute but beings the code runs
does not provide a clear explaination why it runs now. In short

Original line from MSDN example
<section name="mySection"
type="System.Configuration.NameValueSectionHandler", System />

I removed "System"
<section name="mySection"
type="System.Configuration.NameValueSectionHandler" />

Did a rebuild and was able to retrieve key values from the app.config file.

Also when attempting to get the code to work I did extensive Googling to see
if others had a better method to reading a <SectionGroup> and found
http://authors.aspalliance.com/aspxtreme/webapps/creatingnewsectionhandlers.aspx
http://www.codeguru.com/columns/dotnet/article.php/c7987__2/

The CodeGuru article states that he had some problems as in this paragraph
"Note the extra "Version," "Culture," and "PublicKeyToken" specified in
defining a section group. These all come from what the machine.config
configures on the local machine. Occasionally, it has worked for me without
them, but other times an exception is thrown that it can't create the
appropriate handler. I've found that if I include those items, it
consistently works. "
which is different then what I encountered.

Kevin
 
Hi Kevin,

Interesting. I have done this before, and I don't remember what (if any)
difficulties I may have had, but I used the full set of attributes from the
Machine.Config file, and had no problems. I'm glad to see you resolved it,
though.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
I had tried the settings from Machine.Config but still had problems.

Thanks for your assistance, as always these forums are excellent resources
with plenty of talented people.
 
If you had set the Copy Local Property to True for the Reference System
then it should work without you removing the word System.
 
Back
Top